Function Examples: 47 Problems with Answers

Worked examples of Function to try before you open each solution, then practice problems with answers.

Worked Examples

Example 1

medium
Trace: `def f(n): total=1; for i in range(1, n+1): total*=i; return total`. What is `f(5)`?
Show solution

Answer

120

First step

1.
This computes 5!=1⋅1⋅2⋅3⋅4⋅5.

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Free: full solutions on 3 topics a day. No card.

Every topic, every day: the Family plan

Example 2

medium
Trace keyword args: `def f(a, b=2, c=3): return a + b + c` called as `f(1, c=10)`. What is the result?

Example 3

hard
Trace recursion: `def gcd(a, b): return a if b==0 else gcd(b, a%b)`. What is `gcd(48, 18)`?

Example 4

hard
Trace higher-order: `def apply(f, x): return f(x)`. With `def sq(x): return x*x`, what is `apply(sq, 6)`?

Practice Problems

Try each one, then open the solution to compare your method.

Example 1

easy
Trace: `def add(a,b): return a+b`. What does `add(2,3)` return?

Example 2

easy
Trace: `def f(x): return x*x`. What is `f(4)`?

Example 3

easy
In `def greet(name): ...`, is `name` a parameter or an argument?

Example 4

easy
Trace: `def f(): return 7`. What does calling `f()` give, and what does merely DEFINING f do at runtime?

Example 5

easy
Trace: `def f(a, b=10): return a+b`. What is `f(5)`?

Example 6

easy
Trace: `def f(a, b=10): return a+b`. What is `f(5, 2)`?

Example 7

easy
What does a function with no `return` statement give back when called in Python?

Example 8

easy
Trace: `def double(x): return 2*x`. What is `double(double(3))`?

Example 9

medium
Trace: `def f(n): if n<=1: return 1; return n*f(n-1)`. What is `f(4)`?

Example 10

medium
Trace: `def f(x): x=x+1; return x` with `y=5; z=f(y)`. After the call, what are y and z?

Example 11

medium
Trace: `def f(): return 1; print('after')`. What does `f()` return, and does 'after' print?

Example 12

medium
Trace: `def f(a, b, c): return a - b - c`. What is `f(10, 3, 2)`?

Example 13

medium
Trace: `def f(lst): lst.append(9)` with `data=[1,2]; f(data)`. What is data afterward?

Example 14

medium
Trace: `def f(x): return x % 2 == 0`. What is `f(7)`?

Example 15

medium
Trace: `def f(n): total=0; for i in range(1,n+1): total+=i; return total`. What is `f(5)`?

Example 16

medium
Trace: `def f(s): return len(s) > 3` with `f('hi')`. Result?

Example 17

medium
Trace: `def f(a, b): return a, b` with `x, y = f(3, 4)`. What are x and y?

Example 18

challenge
Trace recursion: `def fib(n): if n<2: return n; return fib(n-1)+fib(n-2)`. What is `fib(5)`?

Example 19

challenge
Mutable default trap: `def f(x, acc=[]): acc.append(x); return acc`. Calling `f(1)` then `f(2)` (no acc passed) returns what each time?

Example 20

challenge
Trace scope: `x=1; def f(): x=2; return x; result=f()`. After running, what are the global x and result?

Example 21

easy
Trace: `def cube(x): return xxx`. What is `cube(3)`?

Example 22

easy
Trace: `def sub(a, b): return a - b`. What is `sub(9, 4)`?

Example 23

easy
Trace: `def neg(x): return -x`. What is `neg(-7)`?

Example 24

easy
Trace: `def avg(a, b): return (a+b)/2`. What is `avg(10, 6)`?

Example 25

easy
Trace: `def first(lst): return lst[0]`. What is `first([7, 8, 9])`?

Example 26

easy
Trace: `def hello(name='World'): return 'Hi ' + name`. What is `hello()`?

Example 27

easy
Trace: `def f(x): return x + 1`. What is `f(f(1))`?

Example 28

medium
Trace: `def f(s): return s.upper()`. What is `f('hi')`?

Example 29

medium
Trace: `def f(x, y): return x if x > y else y`. What is `f(3, 7)`?

Example 30

medium
Trace: `def f(lst): return [x*2 for x in lst]`. What is `f([1, 2, 3])`?

Example 31

medium
Trace: `def f(n): return n*(n+1)//2`. What is `f(10)`?

Example 32

medium
Trace: `def f(*args): return sum(args)`. What is `f(1, 2, 3, 4)`?

Example 33

medium
Trace: `def f(x): return x.split(',')`. What is `f('a,b,c')`?

Example 34

medium
Trace: `def f(d, k): return d.get(k, 0)`. What is `f({'a':5}, 'b')`?

Example 35

medium
Trace: `def f(n): r=1; while n>1: r*=n; n-=1; return r`. What is `f(4)`?

Example 36

medium
Trace closure: `def make_adder(k): def add(x): return x+k; return add; f=make_adder(10)`. What is `f(5)`?

Example 37

medium
Trace: `def f(a, b, c): return max(a, b, c)`. What is `f(3, 9, 5)`?

Example 38

medium
Trace: `def f(s): return s == s[::-1]`. What is `f('racecar')`?

Example 39

hard
Trace: `def f(lst): return sum(x for x in lst if x % 2 == 0)`. What is `f([1, 2, 3, 4, 5, 6])`?

Example 40

hard
Trace scope and side-effects: `count = 0; def bump(): global count; count += 1; bump(); bump(); bump()`. What is `count`?

Example 41

hard
Trace: `def f(lst): return lst[::2]`. What is `f([10, 20, 30, 40, 50])`?

Example 42

challenge
Trace recursion with two base cases: `def f(n): return n if n<2 else f(n-1)+f(n-2)`. What is `f(6)`?

Example 43

challenge
Trace aliasing: `def f(d): d['x'] = 1; data = {}; f(data); print(data)`. What is printed and why?

What to focus on

Core idea: Functions break programs into manageable pieces, enable reuse, and reduce repetition.

Common stuck point: Defining a function doesn't run it — you must call it. Parameters (defined) differ from arguments (passed in).

Ask yourself: Are the same steps needed in more than one place or with different inputs, so they should be written once under a name?

Concept recap

A named, reusable block of code that performs a specific task and can optionally accept inputs (parameters) and return a result. Functions allow you to organize code into logical units, write a solution once, and invoke it from anywhere in the program.

A vending machine with a name on the front works like a function. You press a button with your choice (the argument), the same machinery inside does the work every time, and an item drops out (the return value); you never need to see inside to use it. The comparison stops at functions that only do a job, such as drawing a shape, and hand back nothing useful.

Read the full lesson on Function

Common mistakes to watch for

Background knowledge

These ideas may be useful before you work through the harder examples.

function programmingreturn valuesmodular design