Worked examples of Parameters to try before you open each solution, then practice
problems with answers.
Worked Examples
Example 1
easy
What is the output of the following? FUNCTION greet(name): OUTPUT 'Hello, ' + name. greet('Alice'). greet('Bob').
Show solution
Answer
Hello, Alice then Hello, Bob.
First step
1.
Step 1: When greet('Alice') is called, the parameter name takes the value 'Alice'.
Full solution
2.
Step 2: The function outputs 'Hello, ' + 'Alice' = 'Hello, Alice'.
3.
Step 3: When greet('Bob') is called, name = 'Bob', so it outputs 'Hello, Bob'. Parameters allow the same function to work with different inputs.
Parameters are variables listed in a function definition that receive values (arguments) when the function is called. They make functions reusable with different data.
Example 2
medium
FUNCTION calculateArea(length, width): RETURN length * width. What is calculateArea(5, 3)? What happens if you call calculateArea(5) with only one argument?
Example 3
easy
Pattern: same function, different arguments. `def square(n): return n*n`. Trace `square(3)`, `square(7)`, `square(10)`.
Example 4
medium
Trace: `def push(items, x): items.append(x); return items`. After `lst=[1,2]; push(lst, 9)`, what is `lst`?
Example 5
medium
Same code, different data. `def total(prices, tax): return sum(prices) * (1 + tax)`. Compute `total([10, 20, 30], 0.1)`.
Example 6
hard
Trace: `def f(n, acc=0): if n == 0: return acc; return f(n-1, acc+n)`. What is `f(4)`?
Example 7
challenge
Design a function `clamp(value, lo=0, hi=100)` that returns value bounded between lo and hi. Trace `clamp(150)` and `clamp(-3, lo=-10, hi=10)`.
Practice Problems
Try each one, then open the solution to compare your method.
Example 1
medium
Write a function called power(base, exponent) that returns base raised to the exponent using a loop. Trace power(2, 4).
Example 2
hard
Explain the difference between passing by value and passing by reference. If FUNCTION double(x): x = x * 2 is called with SET a = 5. double(a), what is the value of a after the call in each case?
Example 3
easy
In `def f(name): print(name)`, what is `name`?
Example 4
easy
`def f(a, b): return a - b`. What is `f(10, 3)`?
Example 5
easy
`def f(a, b): return a - b`. What is `f(3, 10)`?
Example 6
easy
How many parameters does `def f(x, y, z): return 0` have?
Example 7
easy
`def f(a, b=5): return a + b`. What is `f(2)`?
Example 8
easy
What is the difference between a parameter and an argument in `f(x)` called as `f(7)`?
Example 9
easy
`def f(a, b, c): return a`. What does `f(1, 2, 3)` return?
Example 10
easy
Calling `def f(a, b): return a+b` as `f(5)` causes what?
Example 11
medium
`def f(a, b, c): return a + 2b + 3c`. What is `f(1, 2, 3)`?
Example 12
medium
Using keyword arguments, `def f(a, b): return a - b` called as `f(b=2, a=10)` returns what?
Example 13
medium
`def f(*nums): return sum(nums)`. What is `f(1, 2, 3, 4)`?
Example 14
medium
`def scale(x, factor=2): return x*factor`. Calls: `scale(5)`, `scale(5, 3)`. What are the two results?
Example 15
medium
Why does passing `f(2, 3)` to `def f(width, height): return width*height` work, but the variable names at the call site don't matter?
Example 16
medium
`def f(a, b): a = a + 1; return a + b`. After `x = 5; f(x, 1)`, what is `x`?
Example 17
medium
`def f(a, b, c=0): return a+b+c`. Which call is INVALID: `f(1,2)`, `f(1,2,3)`, `f(1)`?
Example 18
challenge
`def swap(a, b): a, b = b, a` is called as `swap(x, y)` with `x=1, y=2`. After the call, what are `x` and `y`?
Example 19
challenge
`def f(items=[]): items.append(1); return len(items)`. What does the THIRD call `f()` return?
Example 20
challenge
A function takes 4 parameters: `(host, port, user, password)`. Why is calling it with keyword arguments safer than positional?
Example 21
medium
`def f(a, b, c): return c`. Called as `f(1, 2, 3)`, what is returned?
Example 22
medium
`def f(a, b): return a * 10 + b`. What is `f(3, 7)`?
Example 23
easy
`def add(x, y): return x + y`. What is `add(4, 6)`?
Example 24
easy
How many parameters does `def area(base, height): return 0.5baseheight` have?
Example 25
easy
`def greet(name): return 'Hi ' + name`. What does `greet('Sam')` return?
Example 26
easy
`def first(a, b, c): return a`. What does `first(7, 8, 9)` return?
Example 27
medium
`def f(a, b, c=10): return a + b + c`. What is `f(1, 2)`?
Example 28
medium
`def f(a, b, c=10): return a + b + c`. What is `f(1, 2, 5)`?
Example 29
medium
`def div(a, b): return a // b`. What is `div(20, 6)`?
Example 30
medium
`def f(a, b): return a - b`. What does the keyword call `f(b=10, a=3)` return?
Example 31
medium
`def f(a, b): a = a + 100; return a + b`. After `x=5; result = f(x, 1)`, what are `x` and `result`?
Example 32
medium
`def f(*args): return len(args)`. What does `f(10, 20, 30, 40)` return?
Example 33
medium
`def speed(distance, time): return distance / time`. A function is meant to compute speed but is called as `speed(2, 60)` when the values are 60 km and 2 hours. What is the bug?
Example 34
medium
`def f(a, b, c): return (a + b) * c`. What is `f(2, 3, 4)`?
Example 35
medium
`def f(name, age=12, grade='A'): return f"{name}-{age}-{grade}"`. What does `f('Lee', grade='B')` return?
Example 36
hard
`def f(x, lst): lst.append(x); x = x + 1; return x`. After `a = 5; b = []; r = f(a, b)`, what are `a`, `b`, `r`?
Example 37
hard
`def add_item(item, bag=[]): bag.append(item); return bag`. What do the FIRST two calls `add_item(1)` then `add_item(2)` return?
Example 38
hard
A function `connect(host, port, user, password)` is called as `connect('db', 'admin', 5432, 'pw')`. Why is this dangerous, and what is the safer call?
Example 39
hard
`def f(a, b, *rest): return a + b + sum(rest)`. What is `f(1, 2, 3, 4, 5)`?
Example 40
challenge
`def f(x, y=x+1): return y`. Why does this definition raise an error at definition time?
What to focus on
Core idea:Parameters make functions flexible—same code, different data.
Common stuck point:Parameter (in definition) vs. argument (in call)—often used interchangeably.
Ask yourself:Is the question about the named slots in a function's definition that get filled by the values in each call?
Concept recap
Named values declared in a function definition that act as placeholders for the actual data (arguments) passed in when the function is called. Parameters allow the same function to operate on different data each time it is invoked.
Parameters are the blank lines on a printed form, labelled 'Name' and 'Age'. The form is the same for everyone; each person fills in different answers, and the office reads each answer by which line it is on. Write your age on the name line and the office gets it wrong, just as swapped arguments do.