 # FAQ: Higher-Order Functions - Functions as Parameters

#1

This community-built FAQ covers the “Functions as Parameters” exercise from the lesson “Higher-Order Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply ( ) below!

Agree with a comment or answer? Like ( ) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

split this topic #2

2 posts were split to a new topic: Better understanding higher order functions - helpful resources

split this topic #5

7 posts were split to a new topic: Crossing wires somewhere with callbacks and HOF’s

split this topic #7

2 posts were split to a new topic: Higher order functions

#8

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??

#9

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.
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.

#10

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.

#11

I have a few questions. Why do we have to put addOneToOne as a parameter? What if remove it as a parameter and still call it? Does timeFuncRuntime count how long it takes to actually run the function or the time between when it is a parameter and it is called?

const timeFuncRuntime = addOneToOne => {
let t1 = Date.now();
let t2 = Date.now();
return t2 - t1;
}

const addOneToOne = () => 1 + 1;

#12

you shouldn’t. The advantage of timeFuncRuntime is that we can calculate the runtime of any function by passing this function as argument when we call timeFuncRuntime:

``````console.log(timeFuncRuntime(addOneToOne));
console.log(timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes));
``````

so `funcParameter` would be a much better name for the parameter of timeFuncRuntime function:

``````const timeFuncRuntime = funcParameter => {
``````

#13

But what does funcParameter do?

#14

`funcParameter` is just a parameter with a logic name.

when we define a function, we can give a function a parameter:

``````const sayHello = (name) => {
console.log(`hello, \${name}`);
}
``````

then when we call the function, we can pass an argument:

``````sayHello('davsalooki')
sayHello('stetim')
``````

in case of `timeFuncRuntime`, we don’t pass a string as argument, but we pass a function, so that in the body `timeFuncRuntime` of we can calculate the runtime of the passed function

#15

Here, I did in the following way by passing simply the defined function and a random value:
const checkThatTwoPlusTwoEqualsFourAMillionTimes = () => {
for(let i = 1; i <= 1000000; i++) {
if ( (2 + 2) != 4) {
console.log('Something has gone very wrong ');
}
}
};

const addTwo = num => num + 2;

const timeFuncRuntime = funcParameter => {
let t1 = Date.now();
funcParameter();
let t2 = Date.now();
return t2 - t1;
};

// Write your code below

const time2p2 = timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes);

const checkConsistentOutput = (time2p2, val) =>{
let firstTry = time2p2(val);
let secondTry = time2p2(val);
if(firstTry === secondTry){
return firstTry;
}else{
return ‘This function returned inconsistent results’;
}

};

#16

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 -

Thanks,
Avril

#17
``````const checkConsistentOutput = (var1, fn) => {
var res1 = fn(var1);
var res2 = fn(var1);
if (res1 == res2) {
return fn(var1)
}
else {
return 'This function returned inconsistent results'
}
}
``````

Maybe someone could explain why code was considered wrong by the autochecker…

#18

Could it be the return value is incorrect? What if you just return res1?

#19

Hello everyone,

I thought the below made sense until I pasted val = 2 into the consistency function and I hit run a few times and it returns numbers like 3, 4, 5… basically for each run a different result. I would expect the val=2 essentially takes the place in addTwo(val=2) and therefore, it should always return 4!

Can anyone help explain please where might be mistake?

// Consistency function evaluates first and second runs are the same
const checkConsistentOutput = (func, val) => {
let firstTry = func(val);
let secondTry = func(val);
if (firstTry === secondTry) {
return firstTry;
} else {
return ‘This function returned inconsistent results’;
}
};

If we set `val = 2` outside of the function, and write `val` as a formal parameter it will shadow the variable and take the value given in the argument. They are not the same variable.