Practice question · Put in order
Order what happens when the call y = sq(2 + 3) runs, where def sq(n): return n * n.
- Store the returned 25 into y
- Evaluate the argument expression 2 + 3, giving 5
- Run the body: compute n * n, giving 25
- Bind the parameter n to the value 5
- Return 25 to the caller
Hints
- The argument expression is fully evaluated before the function is entered.
- The returned value only reaches y after the function hands it back.
Show the answer
- Evaluate the argument expression 2 + 3, giving 5
- Bind the parameter n to the value 5
- Run the body: compute n * n, giving 25
- Return 25 to the caller
- 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.
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
- Whether a function can modify its caller's argument depends on whether the argument is mutable, not on how it…
- For def scale(factor): return factor * 10, later called as scale(60), sort each item by what it is.
- Match each term to its meaning.
- A function with a default argument def f(x, items=[]) behaves strangely: the list keeps its contents between…
- Select every statement about scope that is true.