Codecademy Forums

Higher order functions as parameters terminology

Just a note here… I’ve been following along pretty well but (for whatever reason) this one was really tough lesson. The language in the description was REALLY hard to follow and I’m not even sure this sentence is proper English. “Invoking the function would evaluate to the return value of that function call.”

“EVALUATE” to the return??

14 Likes

Yes, the terminology can be confusing. Here’s the full quote from the lesson:

When we pass a function in as an argument to another function, we don’t invoke it. Invoking the function would evaluate to the return value of that function call. With callbacks, we pass in the function itself by typing the function name without the parentheses (that would evaluate to the result of calling the function)


I can try to explain each part:

When we pass a function in as an argument to another function, we don’t invoke it.

“Invoking” here means making the function run.

Invoking the function would evaluate to the return value of that function call.

“Evaluate to the return value” just means if you call (run) the function, you will get its result back.

With callbacks, we pass in the function itself by typing the function name without the parentheses (that would evaluate to the result of calling the function)

This is saying if you want to pass a function somewhere, you can’t put parentheses after the function name, because you will accidentally call the function instead.


Here is an example of all this, using these two functions from the lesson:

//Returns 2.
function addOneToOne() {
    return 1 + 1;
}

//Measures how long it takes for a function to run.
function timeFuncRuntime(funcParameter) {
   let t1 = Date.now();
   funcParameter();
   let t2 = Date.now();
   return t2 - t1;
}

timeFuncRuntime takes in a parameter named funcParameter. We can pass addOneToOne for that parameter like this:

timeFuncRuntime(addOneToOne);

If we do that, timeFuncRuntime will eventually call addOneToOne when we get to this line:

funcParameter();

And timeFuncFuntime will measure how long it takes for addOneToOne to run – how long it takes it to calculate 1 + 1 and return the number 2.

When we pass a function in as an argument to another function, we don’t invoke it.

Like the quote above says, we don’t invoke (call) addOneToOne when we pass it to timeFuncRuntime. That would look like this:

timeFuncRuntime(addOneToOne());

Notice we have parentheses after addOneToOne this time. That means we are calling addOneToOne, getting the number 2 back, and passing that number to timeFuncFuntime.

It’s the equivalent of doing this:

timeFuncRuntime(2);

That’s not what we want, because timeFuncRuntime is supposed to measure how long it takes for a function to run. The number 2 isn’t a function and can’t run at all.

In fact, we would get this error:

TypeError: funcParameter is not a function
    at timeFuncRuntime [...]

Hope this helps. I know this is a lot of information.

14 Likes

Yes, it was definitely a “mind bending” lesson for me as well!

The statement: “Invoking the function would evaluate to the return value of that function call.” In this exercise we dont want to pass the return value of the function being passed, which is what will happen if the function is passed in as an invoked function. It should be passed without the leading parenthesis. If not the return value of that function will be passed in as a parameter instead.

Yeah I felt quite comfortable up to this point but it felt like my learning curve turned into a near-vertical wall. I can’t help but wonder if this could have been aided with a more practical real-world application, otherwise it risks being clouded by abstraction.

3 Likes

Hi,

I also felt like this lesson was much harder than previous but I persisted to try and learn it. I think I understand after reading tensor2’s explaination so thanks for that!

I do have a question though… when does the below code return the else?

const checkConsistentOutput = (func, val) => {
let checkOne = func(val);
let checkTwo = func(val);
if (checkOne === checkTwo) {
return checkOne;
} else {
return ‘This function returned inconsistent results’;
}
}
I feel like it would always be consistent as the number is added in the call -
checkConsistentOutput(addTwo, 4);

Thanks,
Avril

I am in the same boat as most people here. The language in this lesson is convoluted and the code is hard to understand. I have been doing well with understanding in the web development path, but the javascript section (especially the whale project and higher order functions) has not been all too clear. It is quite demotivating because I am spending more time trying to decode the sidebar on the right then thinking about javascript.

4 Likes

I just feel like I need to say that this is by orders of magnitude the biggest difficulty spike I’ve encountered on this site. I have been smashing my head against this one exercise for 40 minutes and getting absolutely nowhere. I gave up and looked at the solution and even then I still don’t even begin to understand it. I feel like I’ve been following along fine and then I careened into a cliff.

This module probably needs to occur later in the course, because it looks like I’m not the only one having this problem.

3 Likes

This lesson is not very easy to understand. The examples are not very good explained. A such important lesson especially “pro” should be explained a bit better or split them to at least 2 lessons.

1 Like

Consider,

 > function compute(a, b, f) {
       return f(a, b)
<- undefined
 > compute(6, 7, (x, y) => x * y)
<- 42

The function accepts two values, and one function (known as a callback). It then calls the function with the two values as arguments, and returns the outcome.

Whatever function we pass into compute becomes the function that will execute in the return statement.

 > function compute(a, b, f) {
       return f(a, b)
<- undefined
 > compute(42, 7, (x, y) => x / y)
<- 6

In the above we use arrow function syntax to pass in an anonymous function. Using ES5 syntax, it would look like this,

 > function compute(a, b, f) {
       return f(a, b)
<- undefined
 > compute(6, 7, function(x, y) { return x * y })
<- 42

We may compose our callback function ahead of time,

const add = (x, y) => x + y;

then pass in only its reference to the compute function,

 > function compute(a, b, f) {
       return f(a, b)
<- undefined
 > compute(6, 7, add)
<- 13