Bird
Raised Fist0
Pythonprogramming~15 mins

Function call and execution flow in Python - Mini Project: Build & Apply

Choose your learning style10 modes available

Start learning this pattern below

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

  1. Step 1: Understand function call behavior

    When a function is called, the program temporarily moves to the function's code to execute it.
  2. Step 2: Recognize program flow after function

    After the function finishes, the program returns to where it left off and continues running.
  3. Final Answer:

    The program jumps to the function's code and runs it. -> Option C
  4. 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

  1. Step 1: Recall Python function call syntax

    In Python, you call a function by writing its name followed by parentheses, like greet().
  2. Step 2: Eliminate incorrect options

    The incorrect options use keywords or syntax not used in Python for calling functions.
  3. Final Answer:

    greet() -> Option A
  4. 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

  1. Step 1: Understand the function behavior

    The function add takes two numbers and returns their sum.
  2. Step 2: Calculate the function call result

    Calling add(3, 4) returns 3 + 4 = 7, which is stored in result.
  3. Step 3: Print the result

    The print(result) statement outputs 7.
  4. Final Answer:

    7 -> Option B
  5. 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

  1. Step 1: Analyze the print statement

    The code prints greet without parentheses, so it prints the function object, not the result of calling it.
  2. Step 2: Understand function call vs reference

    To run the function and print "Hello", it should be print(greet()) with parentheses.
  3. Final Answer:

    It prints the function object, not the greeting. -> Option A
  4. 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

  1. Step 1: Understand nested function calls

    The function outer defines an inner function inner and calls it, storing its return value.
  2. Step 2: Trace the return values

    inner() returns the string "Inside inner", which outer() then returns.
  3. Step 3: Print the final returned value

    The print(outer()) statement prints "Inside inner".
  4. Final Answer:

    "Inside inner" -> Option D
  5. 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
  • Expecting None because inner is nested
  • Assuming error due to nested function