Module 9 · Functions — topic 3 of 19
Function calls
How a call evaluates its arguments, runs the body in a fresh local scope, and produces a value; nesting calls, using return values in expressions, and the call stack.
Learning objectives
- Trace the steps of a function call
- Use return values inside larger expressions
- Explain the call stack when functions call functions
Definition
A function call is an expression of the form name(arguments). Python evaluates the arguments, binds them to the parameters in a new local scope, runs the body, and the call evaluates to the returned value.
Introduction
A call is the moment a function does its work. Understanding the order of events — arguments first, then the body, then the result — lets you predict exactly what a program prints.
Why it matters
Tracing calls is the core skill for debugging. When a traceback shows several lines, each one is a function call waiting on the stack.
Detailed explanation
Arguments are evaluated left to right before the function starts: f(a + 1, g(2)) computes a + 1, then calls g(2), then calls f with both results.
Each call gets its own fresh set of local variables. Calling the same function twice doesn't share locals between the calls.
When a function calls another, the caller waits. Python keeps track of the waiting calls on the call stack; the most recent call finishes first. A traceback is a printout of this stack.
Because a call evaluates to a value, it can appear anywhere an expression can: in arithmetic, as an argument, in a condition or in an f-string.
Calling a function object needs parentheses. Writing the name alone (greet instead of greet()) just refers to the function without running it.
Syntax
- Arguments are evaluated before the call.
- A call is an expression with a value.
- Without parentheses the function isn't called.
Code examples
Calls inside expressions
25 16 5 squared is 25
Forgetting the parentheses
True 12:00 function
More examples
Watching the call stack
enter level(1)
enter level(2)
enter level(3)
leave level(3)
leave level(2)
leave level(1)Step-by-step explanation
- level(1) prints its enter line and calls level(2), then waits.
- level(2) does the same and calls level(3).
- level(3) doesn't recurse, so it prints its leave line and returns.
- Control returns to level(2) and then level(1), which print their leave lines: last in, first out.
Expected output
Running “Watching the call stack” prints exactly:
Practical applications
- Tracebacks list the chain of calls that led to an error.
- Pipelines are often written as nested calls: save(clean(load(path))).
Best practices
- Keep nested calls shallow enough to read; use variables for intermediate results.
- Read tracebacks from the bottom (the error) upward (the calls that led to it).
Common mistakes
Missing parentheses
Avoid
Prefer
Without parentheses you test the function object itself, which is always true.
Ignoring a return value
Avoid
Prefer
Many functions return a new value instead of changing their input; keep the result.
Important notes
- Python's default recursion limit is about 1000 nested calls; deeper chains raise RecursionError.
Key takeaways
- Arguments are evaluated first, left to right.
- Each call has its own local variables.
- Calls nest on the call stack and finish in reverse order.
Try it yourself
- 1Predict and then check the output of print(len(str(2 ** 100))).
- 2Write a function that calls two others and trace the order of execution with print statements.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
15 questions cover the “Functions” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?