Bird
Raised Fist0
Pythonprogramming~20 mins

Function call and execution flow in Python - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
๐ŸŽ–๏ธ
Function Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of nested function calls
What is the output of this Python code?
Python
def f(x):
    return x * 2

def g(y):
    return f(y) + 3

print(g(4))
A7
B8
C11
D14
Attempts:
2 left
๐Ÿ’ก Hint
Remember to follow the order of function calls and operations step by step.
โ“ Predict Output
intermediate
2:00remaining
Value of variable after function call
What is the value of variable 'result' after running this code?
Python
def add_one(n):
    return n + 1

result = add_one(5)
A6
B5
CNone
DError
Attempts:
2 left
๐Ÿ’ก Hint
Check what the function returns and what is assigned to 'result'.
โ“ Predict Output
advanced
2:00remaining
Output with recursive function
What is the output of this recursive function call?
Python
def countdown(n):
    if n == 0:
        return 'Done'
    else:
        return str(n) + ' ' + countdown(n-1)

print(countdown(3))
AError
B"3 2 1 Done"
C"Done 3 2 1"
D"3 2 1 0 Done"
Attempts:
2 left
๐Ÿ’ก Hint
Trace the function calls from n=3 down to 0.
โ“ Predict Output
advanced
2:00remaining
Output with function modifying a list
What is printed after this code runs?
Python
def append_number(lst):
    lst.append(4)

numbers = [1, 2, 3]
append_number(numbers)
print(numbers)
A[1, 2, 3, 4]
B[4]
C[1, 2, 3]
DError
Attempts:
2 left
๐Ÿ’ก Hint
Think about how lists are passed to functions in Python.
โ“ Predict Output
expert
2:00remaining
Output of function with default and keyword arguments
What is the output of this code?
Python
def greet(name, greeting='Hello'):
    return f"{greeting}, {name}!"

print(greet('Alice'))
print(greet('Bob', greeting='Hi'))
A"Hi, Alice!\nHi, Bob!"
B"Hi, Alice!\nHello, Bob!"
C"Hello, Alice!\nHello, Bob!"
D"Hello, Alice!\nHi, Bob!"
Attempts:
2 left
๐Ÿ’ก Hint
Check which greeting is used for each call.

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