Skip to content

L04 Calls and returns

15 minSuggested first: L03 Loops and running state

Outcome

Identify caller, callee, argument, parameter, and return value in a nested call.

Learn

A function declaration defines behavior. A call evaluates its arguments, then enters the function with its parameters bound to those values. A return exits that invocation and passes a value back to its caller. Local names in different calls are separate, even when they share a name.

Worked example

What value does charge receive on line 5? Follow the calls in the order they happen, not the order they're written.

JavaScript
function fee(amount) { return amount + 2; }
function invoice(price) {
  return fee(price) + 1;
}
const charge = invoice(4);
Predict first, then show the worked explanation

invoice(4) enters invoice with price = 4. To evaluate its return expression, it calls fee(4). That inner call returns 6. Back in invoice, adding 1 gives 7, which invoice returns. The caller, line 5, receives 7.

Practice and check

Guided practice

Practice, not scored

For invoice(9), what value does the inner call, fee(price), return?

Choose one answer

Exit check

Counts toward completion

In pseudocode, double(x) returns x * 2 and finish(x) returns double(x - 1). When you call finish(6), what argument is passed to double?

Choose one answer

Common mistake

Following functions in the order they're defined in the file. Start at the call site; a definition can sit above or below it.

Next

In the next lesson, see what changes when a function receives a collection rather than a number.

References