Functions — Python Practice Worksheet
A function is a named, reusable block of code. You define it once with def and call it by name as many times as you need. Functions keep programs organized and eliminate repeated code.
Parameters are the variable names listed in the definition — def greet(name): has one parameter, name. Arguments are the actual values passed in a call — greet('Ada') passes the argument 'Ada'. The argument is copied into the parameter when the function runs.
The return statement sends a value back to the caller and ends the function immediately. A function without a return statement gives back None. Printing inside a function is not the same as returning — print shows a value on screen, while return hands a value back for further computation.
Scope controls where variables are visible. A variable created inside a function is local to that function and disappears when it ends; a variable created outside is global to the script. Assigning to a variable inside a function creates a new local variable rather than changing the global one.
Tracing function calls means following the argument values into the parameters, executing the body, and substituting the returned value back at the call site — essential for predicting program output.
Skills practiced
- Defining functions with def
- Distinguishing parameters from arguments
- Using return values in expressions
- Tracing nested function calls
- Predicting local vs global variable behavior
- Knowing when a function returns None
Practice Worksheet: Functions
Instructions: Solve each problem carefully. Show all work clearly. Write your final answer in the space provided or on a separate sheet as directed.
-
1.Write a Python function named is_even that takes one integer parameter n and returns True if n is even and False otherwise.
Functions — Practice Worksheet (continued)
-
2.What is the output of the following code? Show your reasoning. def mystery(x, y): result = x * 2 + y return result print(mystery(3, 5))
Functions — Practice Worksheet (continued)
-
3.Consider the following function definition: def greet(name, greeting="Hello"): return greeting + ", " + name + "!" What will be the output of each of the following function calls? a. greet("Alice") b. greet("Bob", "Hi") c. greet(greeting="Hey", name="Carol")
Functions — Practice Worksheet (continued)
-
4.Write a function named calculate_average that takes a list of numbers as a parameter and returns the average (mean) of those numbers. You may assume the list is not empty.
Functions — Practice Worksheet (continued)
-
5.What is the output of the following code? Explain what happens with the variable x. def modify(x): x = x + 10 return x x = 5 modify(x) print(x)
Functions — Practice Worksheet (continued)
-
6.Which of the following is the correct way to define a function in Python that takes two parameters and returns their sum?
- A. def add(a, b): return a + b
- B. function add(a, b) { return a + b; }
- C. def add(a, b): print(a + b)
- D. def add(a, b) return a + b
Functions — Practice Worksheet (continued)
-
7.Write a function named count_vowels that takes a string as a parameter and returns the number of vowels (a, e, i, o, u) in the string. Your function should work for both uppercase and lowercase letters.
Functions — Practice Worksheet (continued)
-
8.What is the output of the following code? def outer(x): def inner(y): return y * 2 return inner(x) + 1 print(outer(5))
Functions — Practice Worksheet (continued)
-
9.Write a function named find_max that takes a list of numbers as a parameter and returns the largest number in the list. Do not use the built-in max() function.
Functions — Practice Worksheet (continued)
-
10.Consider the following function: def process(data, multiplier=2, offset=0): result = [] for item in data: result.append(item * multiplier + offset) return result What will be the output of each of the following function calls? a. process([1, 2, 3]) b. process([1, 2, 3], 3) c. process([1, 2, 3], offset=5) d. process([1, 2, 3], 3, 1)
Answer Key
-
1.
Final answer: def is_even(n): return n
-
2.
The function mystery computes , which is then printed.Final answer: 11
-
3.
Final answer: a. "Hello, Alice!" b. "Hi, Bob!" c. "Hey, Carol!"
-
4.
Final answer: def calculate_average(numbers): total = 0 for num in numbers: total += num return total / len(numbers)
Answer Key (continued)
-
5.
The variable x inside the function is a local copy. Modifying it inside the function does not affect the global x, so when print(x) is called, it still prints the original value of 5.Final answer: 5
-
6.
Final answer: a
-
7.
Final answer: def count_vowels(s): vowels = "aeiouAEIOU" count = 0 for char in s: if char in vowels: count += 1 return count
-
8.
The inner function inner doubles its argument (), and then the outer function adds 1, giving .Final answer: 11
Answer Key (continued)
-
9.
Final answer: def find_max(numbers): largest = numbers[0] for num in numbers: if num > largest: largest = num return largest
-
10.
Final answer: a. [2, 4, 6] (multiplier=2, offset=0) b. [3, 6, 9] (multiplier=3, offset=0) c. [7, 9, 11] (multiplier=2, offset=5) d. [4, 7, 10] (multiplier=3, offset=1)
Common mistakes to avoid
- Confusing print with return
- Calling a function before it is defined
- Expecting a local variable to exist outside its function
- Passing the wrong number of arguments
- Forgetting that a function without return gives None
Generate your own like this
Want a fresh set of functions problems at your chosen difficulty? Use WorksheetSmith to compile a custom, print-ready PDF in seconds.
Last updated: 2026