Invoking a function's argument?

I’m looking over the review section for Higher Order Functions: https://www.codecademy.com/courses/introduction-to-javascript/lessons/higher-order-functions/exercises/review

My question is, one of the last points of this page, they ask “which variable holds the callback function” followed by “what happens if you invoke the function’s argument”? What do they mean by invoking a function’s argument? I thought the argument always stays within the parentheses of the function?

1 Like

You can pass a function as an argument to another function. This argument function is called a callback function. Callback functions are invoked inside the functions, they are passed to as arguments.

3 Likes

Oh I see. So, in regards to callback functions, on the outside, the callback function is passed as a parameter of a function. Within that function block, when the callback function is invoked inside the function block, does that mean that it is passed as an argument? Thank you so much for the explanation.

1 Like

Yes, you are right. But it’s applied to all functions. When you declare a function and pass some value to it, this value is a parameter. It can be anything: a string, a number, a boolean, a function, etc. Like so:

function myFunc(myString){
  console.log(myString);
}

So myString is a parameter. It’s just a name, a placeholder for a real value that will be passed to the function when it is invoked.

const myValue = "Hello there";
myFunc(myValue);

And above myValue is an argument because it is a real value that is passed to the function myFunc. So “Hello there” will be printed in the console.

The same applies to callback functions.
You first declare a function that accepts another function as a parameter:

function myExample(callbackParam){
  callbackParam();
}

Then you define your callback:

function myCallback () {
  console.log('Hello from callback');
}

Then you invoke myExample with myCallback passed as an argument:

myExample(myCallback);

And you will see “Hello from callback” in the console because myCallback will be invoked during the execution of myExample function.