Callback

HSKwon·2022년 6월 22일
0

콜백함수란...

  • 다른 함수의 인자로 이용되는 함수
  • 어떤 이벤트에 의해 호출되어지는 함수
// The callback method
function meaningOfLife() {
    log("The meaning of life is: 42");
}
// A method which accepts a callback method as an argument
// takes a function reference to be executed when printANumber completes
function printANumber(int number, function callbackFunction) {
    print("The number you provided is: " + number);
}
// Driver method
function event() {
   printANumber(6, meaningOfLife);
}

printANumber()의 두번째 매개변수로 function타입의 callbackFunction을 인자로 받는다.
event()내부에서 printANumber(6, meaningOfLife)를 하고 있는데 meaningOfLife는 printANumber의 매개변수인 callbackFunction에 전달된다. 따라서 meaningOfLife는 콜백함수라고 할 수 있다!

profile
공부한 내용이나 관심 있는 정보를 글로 정리하며 익숙하게 만들고자 합니다.

0개의 댓글