Skip to content

Instantly share code, notes, and snippets.

@RyanJeong
Created August 28, 2019 07:49
Show Gist options
  • Save RyanJeong/d97f7941726d3c90cf96ddbbe2856056 to your computer and use it in GitHub Desktop.
Save RyanJeong/d97f7941726d3c90cf96ddbbe2856056 to your computer and use it in GitHub Desktop.
What is a callback function?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

/*
 *  meaningOfLife():  The callback method 
 */
function meaningOfLife() { 
  log("The meaning of life is: 42"); 
} 

/*
 *  printANumber(): 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); 
} 

/*
 *  event():  Driver method
 */
function event() { 
  printANumber(6, meaningOfLife); 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment