Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Function call and execution flow
๐ Scenario: Imagine you are creating a simple program to greet users by name and show a welcome message. This program will use functions to organize the greeting steps.
๐ฏ Goal: You will build a program with functions that greet a user by name and then display a welcome message. You will see how functions are called and how the program flows from one function to another.
๐ What You'll Learn
Create a function to greet a user by name
Create a function to display a welcome message
Call the greeting function with a specific name
Observe the order of function calls and outputs
๐ก Why This Matters
๐ Real World
Functions help organize code into reusable blocks, making programs easier to read and maintain.
๐ผ Career
Understanding function calls and execution flow is essential for writing clear and efficient code in any programming job.
Progress0 / 4 steps
1
Create a function called greet_user that takes one parameter name and prints "Hello, {name}!"
Write a function named greet_user that accepts a parameter called name. Inside the function, use print(f"Hello, {name}!") to greet the user by name.
Python
Hint
Use def to define the function and print with an f-string to include the name.
2
Create a function called welcome_message that prints "Welcome to our program!"
Write a function named welcome_message that takes no parameters and prints "Welcome to our program!" when called.
Python
Hint
Define a function with no parameters and use print inside it.
3
Call the function greet_user with the argument "Alice" and then call welcome_message
Write two lines of code: first call the function greet_user with the argument "Alice", then call the function welcome_message with no arguments.
Python
Hint
Call each function by its name followed by parentheses. Pass "Alice" as argument to greet_user.
4
Print the output of the program to see the greeting and welcome message
Run the program so it prints the greeting for "Alice" and the welcome message. The output should show both messages in order.
Python
Hint
Just run the program. The two print statements inside the functions will show the messages.
Practice
(1/5)
1. What happens when you call a function in Python?
easy
A. The program restarts from the beginning.
B. The program stops running completely.
C. The program jumps to the function's code and runs it.
D. The function code is ignored.
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 C
Quick 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
A. greet()
B. call greet()
C. function greet()
D. run greet()
Solution
Step 1: Recall Python function call syntax
In Python, you call a function by writing its name followed by parentheses, like greet().
Step 2: Eliminate incorrect options
The incorrect options use keywords or syntax not used in Python for calling functions.
Final Answer:
greet() -> Option A
Quick 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
A. 34
B. 7
C. None
D. Error
Solution
Step 1: Understand the function behavior
The function add takes two numbers and returns their sum.
Step 2: Calculate the function call result
Calling add(3, 4) returns 3 + 4 = 7, which is stored in result.
Step 3: Print the result
The print(result) statement outputs 7.
Final Answer:
7 -> Option B
Quick 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
A. It prints the function object, not the greeting.
B. SyntaxError due to missing parentheses in function definition.
C. NameError because greet is not defined.
D. IndentationError inside the function.
Solution
Step 1: Analyze the print statement
The code prints greet without 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 be print(greet()) with parentheses.
Final Answer:
It prints the function object, not the greeting. -> Option A
Quick 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
A. Error
B. inner
C. None
D. "Inside inner"
Solution
Step 1: Understand nested function calls
The function outer defines an inner function inner and calls it, storing its return value.
Step 2: Trace the return values
inner() returns the string "Inside inner", which outer() then returns.
Step 3: Print the final returned value
The print(outer()) statement prints "Inside inner".
Final Answer:
"Inside inner" -> Option D
Quick 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