Computer Science I / Functions and Parameter Passing
Practice question · Put in order

Order what happens when the call y = sq(2 + 3) runs, where def sq(n): return n * n.

Hints
  1. The argument expression is fully evaluated before the function is entered.
  2. The returned value only reaches y after the function hands it back.
Show the answer
  1. Evaluate the argument expression 2 + 3, giving 5
  2. Bind the parameter n to the value 5
  3. Run the body: compute n * n, giving 25
  4. Return 25 to the caller
  5. Store the returned 25 into y
Why

The argument 2 + 3 is computed to 5 first, then bound to n, the body runs, 25 is returned, and only then is it stored into y. The parameter n takes the argument's value at binding time, it is not linked to the expression 2 + 3 afterwards.

Read the lesson: Functions and Parameter Passing →

Practise Functions and Parameter Passing

The app has 5 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Functions and Parameter Passing