Function Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Function.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

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.

Like a recipe: you name it, write it once, and call it whenever you need it.

Read the full concept explanation β†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

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).

Sense of Study hint: When creating a function, give it a descriptive name that tells what it does. Define parameters for any data it needs from the caller. Use return to send a result back. Keep each function focused on a single taskβ€”if it does too many things, split it into smaller functions.

Common Mistakes to Watch For

Before you work through the examples, skim the mistake guide so you know which shortcuts and sign errors to avoid.

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)`?

Answer

120120

First step

1
This computes 5!=1β‹…1β‹…2β‹…3β‹…4β‹…55! = 1 \cdot 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5.

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

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan β€” every worked solution, all subjects

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 these problems on your own first, 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 x*x*x`. 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?

Background Knowledge

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

function programmingreturn valuesmodular design