Functions help us organize code into small tasks. Calling a function runs its code step-by-step.
Function call and execution flow in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
def function_name(parameters): # code block function_name(arguments)
Define a function with def and give it a name.
Call the function by writing its name followed by parentheses.
Examples
greet that prints a message. Calling greet() runs the print.Python
def greet(): print("Hello!") greet()
add(3, 4) prints 7.Python
def add(a, b): print(a + b) add(3, 4)
Python
def say_name(name): print(f"Your name is {name}.") say_name("Anna")
Sample Program
This program shows how calling functions controls the flow. start() runs first, then calls step_one() and step_two() in order.
Python
def start(): print("Start of program") step_one() step_two() print("End of program") def step_one(): print("Step one running") def step_two(): print("Step two running") start()
Important Notes
When a function is called, the program jumps inside it and runs its code.
After the function finishes, the program goes back to where it was called.
Functions help keep code clean and easy to follow.
Summary
Functions are blocks of code you can run by calling their name.
Calling a function makes the program jump to that function's code and run it.
After the function finishes, the program continues where it left off.
Practice
1. What happens when you call a function in Python?
easy
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]
Hint: Calling a function runs its code then returns [OK]
Common Mistakes:
- Thinking the program stops after a function call
- Believing the function code is skipped
- Assuming the program restarts after calling a function
2. Which of the following is the correct way to call a function named
greet in Python?easy
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]
Hint: Call functions by name followed by parentheses [OK]
Common Mistakes:
- Adding extra keywords like 'call' or 'run'
- Using 'function' keyword to call
- Forgetting parentheses after function name
3. What is the output of this code?
def add(x, y):
return x + y
result = add(3, 4)
print(result)medium
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]
Hint: Return value is printed, so output is sum 7 [OK]
Common Mistakes:
- Thinking the function prints instead of returns
- Concatenating numbers as strings (34)
- Expecting None because of missing print inside function
4. Find the error in this code:
def greet():
print("Hello")
print(greet)medium
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]
Hint: Use parentheses to call function, else prints object [OK]
Common Mistakes:
- Thinking missing parentheses cause syntax error
- Assuming function is not defined
- Confusing function call with function reference
5. Given this code, what will be printed?
def outer():
def inner():
return "Inside inner"
result = inner()
return result
print(outer())hard
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]
Hint: Nested function returns value used by outer function [OK]
Common Mistakes:
- Thinking inner function name prints instead of its return
- Expecting None because inner is nested
- Assuming error due to nested function
