function 함수이름(매개변수){
...
return 리턴값;
}
function power(x){
retrun x*x;
}
console.log(power(10));
console.log(power(11));
실행결과
100
121
function power(x, y){
retrun x*y;
}
console.log(power(10, 2));
console.log(power(11, 3));
실행결과
20
33
function print(message){
console.log(`"${message}"`);
}
print("안녕하세요");
print("자바스크립트");
실행결과
"안녕하세요"
"자바스크립트"
자바스크립트는 함수를 변수에 저장할 수 있어 함수를 함수의 매개 변수로 전달할 수 있다.
function callTenTimes(callback){
//10회 반복
for (let i = 0; i < 10; i++){
//매개 변수로 전달된 함수를 호출합니다.
callback();
}
}
callTenTimes(function(){
console.log('함수 호출');
});
함수 호출
함수 호출
함수 호출
함수 호출
함수 호출
함수 호출
함수 호출
함수 호출
함수 호출
함수 호출