JavaScript

[JavaScript] 연산결과 리턴하기(Operators)

sagesse2021 2021. 11. 7. 20:35
반응형
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
// fun1() 함수 정의 
// 2개의 변수에 값을 저장하고  
// 변수를 이용해서  + - * / 결과 출력  리턴값 없음 
function fun1() {
	a=10;
	b=20;
	alert(a+b);
	alert(a-b);
	alert(a*b);
	alert(a/b);
	return;
}
function fun2(a,b) {
	alert(a+b);
	alert(a-b);
	alert(a*b);
	alert(a/b);
	return;
}
function fun3(x,y) {
	//사각형면적 가로*세로
// 	alert("사각형의 면적 : "+x*y);
	return x*y;
}
// 숫자값 하나을 받아서 3*x+10 => 계산결과 리턴
function fun4(x) {
	return 3*x+10;
}
// 반지름을 받아서 원면적구해서 => 결과값 리턴
function fun5(r) {
	return r*r*3.14;
}
</script>
</head>
<body>
<h1>js1/test5.html</h1>
<input type="button" value="사칙연산1" onclick="fun1()">
<input type="button" value="사칙연산2" onclick="fun2(10,20)">
<input type="button" value="사칙연산2" onclick="fun2(100,200)">
<input type="button" value="사각형면적" onclick="alert('사각형의 면적 : '+fun3(10,20))">
<input type="button" value="삼각형면적" onclick="alert('삼각형의 면적 : '+fun3(10,20)/2)">
<input type="button" value="함수계산결과" onclick="alert('함수결과값 : '+fun4(5))">
<input type="button" value="원의면적" onclick="alert('원의면적'+fun5(10))">
</body>
</html>

//function함수에 변수를 저장한 fun1()과 fun2(10,20)은 동일한 결과 출력
//fun2()에 각각 다른 값을 넣으면 각각 다른 결과 출력
//fun3()에 값을 넣고 function함수에서 연산결과를 리턴받아서 실행문 출력
//결과값 리턴받아서 연산도 가능

반응형