Function call and execution flow in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we call a function, the computer runs the code inside it. We want to know how the time it takes changes as the input grows.
How does the number of steps grow when the function runs with bigger inputs?
Analyze the time complexity of the following code snippet.
def print_numbers(n):
for i in range(n):
print(i)
print_numbers(5)
This function prints numbers from 0 up to n-1. It runs a loop that repeats n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that prints each number.
- How many times: It runs exactly n times, once for each number from 0 to n-1.
As n gets bigger, the loop runs more times, so the total steps grow in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: Doubling n doubles the work because each number is printed once.
Time Complexity: O(n)
This means the time grows in direct proportion to the input size n.
[X] Wrong: "The function runs in constant time because it just calls print."
[OK] Correct: The print happens inside a loop that runs n times, so the total time depends on n, not just one step.
Understanding how function calls and loops affect time helps you explain your code clearly and shows you know how programs grow with input size.
"What if we added another loop inside the function that also runs n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand function call behavior
When a function is called, the program temporarily moves to the function's code to execute it.Step 2: Recognize program flow after function
After the function finishes, the program returns to where it left off and continues running.Final Answer:
The program jumps to the function's code and runs it. -> Option CQuick Check:
Function call = program runs function code [OK]
- Thinking the program stops after a function call
- Believing the function code is skipped
- Assuming the program restarts after calling a function
greet in Python?Solution
Step 1: Recall Python function call syntax
In Python, you call a function by writing its name followed by parentheses, likegreet().Step 2: Eliminate incorrect options
The incorrect options use keywords or syntax not used in Python for calling functions.Final Answer:
greet() -> Option AQuick Check:
Function call syntax = name + () [OK]
- Adding extra keywords like 'call' or 'run'
- Using 'function' keyword to call
- Forgetting parentheses after function name
def add(x, y):
return x + y
result = add(3, 4)
print(result)Solution
Step 1: Understand the function behavior
The functionaddtakes two numbers and returns their sum.Step 2: Calculate the function call result
Callingadd(3, 4)returns 3 + 4 = 7, which is stored inresult.Step 3: Print the result
Theprint(result)statement outputs 7.Final Answer:
7 -> Option BQuick Check:
3 + 4 = 7 [OK]
- Thinking the function prints instead of returns
- Concatenating numbers as strings (34)
- Expecting None because of missing print inside function
def greet():
print("Hello")
print(greet)Solution
Step 1: Analyze the print statement
The code printsgreetwithout parentheses, so it prints the function object, not the result of calling it.Step 2: Understand function call vs reference
To run the function and print "Hello", it should beprint(greet())with parentheses.Final Answer:
It prints the function object, not the greeting. -> Option AQuick Check:
Missing () means function object printed [OK]
- Thinking missing parentheses cause syntax error
- Assuming function is not defined
- Confusing function call with function reference
def outer():
def inner():
return "Inside inner"
result = inner()
return result
print(outer())Solution
Step 1: Understand nested function calls
The functionouterdefines an inner functioninnerand calls it, storing its return value.Step 2: Trace the return values
inner()returns the string "Inside inner", whichouter()then returns.Step 3: Print the final returned value
Theprint(outer())statement prints "Inside inner".Final Answer:
"Inside inner" -> Option DQuick Check:
Nested call returns inner's string [OK]
- Thinking inner function name prints instead of its return
- Expecting None because inner is nested
- Assuming error due to nested function
