Function (Programming) Examples: 47 Problems with Answers

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

Worked Examples

Example 1

medium
Define a function `double(n)` that returns n × 2. Then trace `double(double(3))`.
Show solution

Answer

12

First step

1.
Step 1: FUNCTION double(n): RETURN n * 2.

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
Why is it better to write a function `calculateTax(amount, rate)` than to repeat the tax calculation code everywhere?

Example 3

medium
`def f(x): return x*x + 1`. What is `f(3)`?

Example 4

medium
`def inc(n): return n+1` and `def dbl(n): return n*2`. What is `dbl(inc(4))`?

Example 5

medium
`def avg(a, b, c): return (a+b+c)/3`. What is `avg(2, 4, 9)`?

Example 6

medium
`def f(x): if x < 0: return 'neg'; else: return 'pos'`. What does `f(0)` return?

Example 7

medium
`def f(): x = 10; return x` and `print(x)` after the call. What happens?

Example 8

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

Example 9

hard
`def f(n): return 1 if n<=1 else n * f(n-1)`. What is `f(5)`?

Example 10

hard
`def f(n): if n<2: return n; return f(n-1) + f(n-2)`. What is `f(6)`? (Fibonacci with f(0)=0,f(1)=1)

Example 11

hard
`def f(lst): lst.append(99)` is called with `data=[1,2,3]; f(data)`. What is `data` afterward?

Example 12

challenge
`def f(n, acc=1): return acc if n==0 else f(n-1, acc*n)`. What is `f(4)`?

Practice Problems

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

Example 1

medium
Write a function `isEven(n)` that returns TRUE if n is even, FALSE otherwise. Trace it for n = 7.

Example 2

medium
Given `FUNCTION toSeconds(minutes): RETURN minutes * 60`, what does `toSeconds(3)` return, and why is using a function better than rewriting `minutes * 60` everywhere?

Example 3

easy
A function is defined as `def greet(): print("hi")`. The program never prints anything. Why?

Example 4

easy
What does this function return: `def f(): return 3 + 4`?

Example 5

easy
In `def area(w, h): return w * h`, what are `w` and `h` called?

Example 6

easy
Why use a function instead of copying the same 5 lines of code three times?

Example 7

easy
What prints? `def f(x): return x * 2` then `print(f(5))`

Example 8

easy
Does `def f(): x = 10` make `x` usable outside the function after calling `f()`?

Example 9

easy
How many times does `greet()` run the body of `greet` if you write `greet()` then `greet()`?

Example 10

easy
What is the output of `def f(): print("A"); return; print("B")` when called?

Example 11

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

Example 12

medium
`def f(n): return n + 1` and `def g(n): return n * 3`. What is `g(f(2))`?

Example 13

medium
A function should return the larger of two numbers. `def big(a,b): if a > b: return a` is buggy. What does `big(2, 5)` return?

Example 14

medium
`def f(x): return x` and we call `f("hi")`, `f(3)`, `f(True)`. How many distinct return values are there?

Example 15

medium
What prints? `def f(): return 1` then `print(f)`

Example 16

medium
`def count(): count.n += 1; return count.n` with `count.n = 0`. What does the third call return?

Example 17

medium
Single-responsibility: which is the better-designed function pair? (A) one `doEverything()`, or (B) `readData()` and `formatData()`?

Example 18

medium
`def f(a, b=10): return a + b`. What does `f(5)` return?

Example 19

challenge
`def f(n): return n if n < 2 else f(n-1) + f(n-2)`. What is `f(4)`? (This is Fibonacci with f(0)=0, f(1)=1.)

Example 20

challenge
A function `f(lst)` appends to `lst` and is called with the same list 3 times. Why might results surprise you?

Example 21

challenge
Design: you must compute `tax(price)` in 10 places. Today the rate is 8%. Tomorrow it changes to 9%. Why is a function the right tool?

Example 22

medium
`def f(): return; print("after")` is called. What does it return and does it print?

Example 23

easy
What does `def f(): return 12 - 5` return?

Example 24

easy
`def triple(n): return n * 3`. What is `triple(4)`?

Example 25

easy
A function is defined but never called. What happens when the program runs?

Example 26

easy
`def f(): print('A'); print('B')`. Calling `f()` prints what?

Example 27

easy
`def f(): pass`. What does `f()` return?

Example 28

medium
`def f(x, y=5): return x + y`. What does `f(2)` return?

Example 29

medium
A function `bigger(a, b)` should return the larger. The code `def bigger(a,b): if a>b: return a` is incomplete. What does `bigger(2,5)` return?

Example 30

medium
`def mystery(a, b): return a if a>b else b`. What does `mystery(7, 12)` return?

Example 31

medium
Why might using a function be safer than copying the same 6-line block 5 times across a program?

Example 32

medium
`def f(x): return x; print('done')`. When called, what is printed by the function body?

Example 33

hard
`def f(x, lst=[]): lst.append(x); return lst`. Calling `f(1)` then `f(2)` returns what for the second call?

Example 34

hard
`def f(x): x = x + 1` is called as `f(5)`. After the call, what is the value of the outer 5?

Example 35

challenge
You must compute `taxedPrice(p)` in 12 places. The tax rate may change yearly. Why is a function clearly the right tool?

What to focus on

Core idea: Write once, use many times. Functions take input (parameters) and give output (return value).

Common stuck point: Defining a function doesn't run it—you must call the function.

Ask yourself: Is a named block written once and then run by calling it, maybe with inputs and a value sent back?

Concept recap

A named, reusable block of code that performs a specific task, taking input (parameters) and optionally returning output (a return value). Functions allow you to write a piece of logic once and use it many times throughout a program.

A function is like a recipe card filed under a name. Writing the card cooks nothing; cooking happens only when someone pulls the card out and follows it (the call), using today's ingredients (the arguments), and hands over the finished dish (the return value). The same card works every day, with different ingredients each time.

Read the full lesson on Function (Programming)

Background knowledge

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

algorithmabstraction