Loading video player…

Pass Functions to Functions

In this lesson, you gave Tomás a countdown for the start of the film, and let the caller decide what happens at each count by handing it a function.

Key Points

  • A function is a value, so you can pass it to another function as an argument, like a number or a string.
  • The function that receives it decides when to call it and with which arguments. In countdown(), the loop calls announce with each count, so you never call sayCount yourself. Each count arrives as the callback’s argument, whatever the callback names its parameter. In sayCount, that parameter is number.
  • A function you hand to another function, for it to call later, is a callback.
  • You can pass a named function, such as sayCount, or write an arrow right in the call. The arrow’s (count) is its parameter list, and its body runs only when countdown() calls it.
  • With no parentheses after the function’s name, you hand over the function. With parentheses after it, you call it now and hand over whatever it returns.

Watch the Parentheses

Passing sayCount() instead of sayCount runs sayCount() right away, with no argument, so its parameter number is undefined. It logs undefined... and, with no return statement, gives back undefined. That undefined lands in announce, so the loop fails when it tries to call it:

Language: JavaScript
countdown(3, sayCount());

Running the script prints one line, then stops with an error. Here’s the output, with the file path and most of the stack trace shortened:

Language: Program Output
undefined...
.../countdown.js:3
    announce(count);
    ^

TypeError: announce is not a function
    at countdown (.../countdown.js:3:5)
    ...

The error points inside countdown(), but the mistake is in the call.

Final Code

Language: JavaScript Filename: countdown.js
function countdown(n, announce) {
  for (let count = n; count > 0; count--) {
    announce(count);
  }
}

function sayCount(number) {
  console.log(`${number}...`);
}

countdown(3, sayCount);
countdown(3, (count) => console.log(`Coco starts in ${count}`));

Run it from your terminal, and each call counts down from three:

Language: Shell
$ node countdown.js
3...
2...
1...
Coco starts in 3
Coco starts in 2
Coco starts in 1

The loop in countdown() is the same for both calls. Only the callback you hand it changes what each count prints.

Next, in Give Parameters Default Values, you’ll give a parameter a value to use when the call leaves it out.

00:00 In the last lesson, you stored Tomás’s helpers in variables, because a function is a value. In this one, you’ll hand a function to another function, and watch who calls it.

00:11 Tomás wants a countdown in the terminal before the film starts. Here’s a function for it. Its parameter, n, says where to start. The loop counts down from n to one and logs each count with three dots.

00:25 Run the file, and countdown prints three, two, one.

00:30 Now Tomás wants a second countdown that names the film, like “Coco starts in three”. He could copy the function. However, only one line would change, the one that does something with each count.

00:43 What if the caller could hand that line in, as a function?

00:48 Give countdown a second parameter, named announce, for the job to do at each count. It will hold a function. In the last lesson, you called a function straight from the helpers array, because anything that holds a function can be called with parentheses.

01:02 So inside the loop, countdown calls announce with the current count.

01:08 Then write the job itself. A function named sayCount takes a number and logs it with three dots, the line you lifted out. Now the call. You usually call a function by writing its name with parentheses, so you might pass sayCount that way too, with parentheses.

01:25 Your turn: pause the video and predict what happens when this runs.

01:35 It prints undefined with three dots, once. Then it stops with a TypeError: announce is not a function.

01:44 The error points at the loop inside countdown. But the mistake is on the last line. Follow the call. The parentheses run sayCount right away, before countdown even starts.

01:56 It gets no argument, so its parameter, number, is undefined, and it logs undefined with three dots. It has no return statement, so undefined is the call’s value.

02:08 Countdown receives that undefined in its parameter announce, in place of a function. So when the loop tries to call announce with three, there’s no function to call.

02:20 Drop the parentheses. Now you pass sayCount itself, the function value, and inside countdown, announce holds it.

02:29 Run the file again, and it prints three, two, one. Follow the call once more. Countdown starts with n set to three. The loop calls announce with three, and that runs sayCount with three as its argument.

02:45 The names don’t need to match. Three lands in sayCount’s own parameter, number, so it logs three. Then the loop calls it with two, and with one. You never call sayCount yourself.

02:58 Countdown decides when to call it, and with which number. A function you hand to another function, for it to call later, is a callback.

03:08 Now Tomás’s second countdown. Instead of writing another named function, write the callback right in the call, as an arrow. It takes the count and logs a line about Coco with it.

03:19 The parentheses around count are the arrow’s parameter list. The console.log inside its body only runs when countdown calls the arrow. Run it, and it prints “Coco starts in three”, then two, then one.

03:35 The loop in countdown didn’t change. Only the function you handed it did. A countdown in the terminal is a small toy, but the picker you’ll build later hands functions over in the same way.

03:47 The rule comes down to the parentheses right after a function’s name. Without them, you hand over the function. With them, you call it now, and hand over whatever it returns.

03:59 You’ve written countdown, a function that takes a callback and calls it with each count. You’ve passed it a named function and an arrow. In the next lesson, Give Parameters Default Values, you’ll give a parameter a value to use when the call leaves it out.

Become a Member to join the conversation.