Practice Parameters in CS Thinking
Use these practice problems to test your method after reviewing the concept explanation and worked examples.
Quick 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.
Showing a random 20 of 50 problems.
Example 1
challenge`def f(x, y=x+1): return y`. Why does this definition raise an error at definition time?
Example 2
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 3
easy`def add(x, y): return x + y`. What is `add(4, 6)`?
Example 4
easy`def f(a, b): return a - b`. What is `f(10, 3)`?
Example 5
mediumWhy 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 6
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 7
medium`def f(name, age=12, grade='A'): return f"{name}-{age}-{grade}"`. What does `f('Lee', grade='B')` return?
Example 8
medium`def f(a, b): return a * 10 + b`. What is `f(3, 7)`?
Example 9
medium`def f(a, b, c): return a + 2b + 3c`. What is `f(1, 2, 3)`?
Example 10
easyPattern: same function, different arguments. `def square(n): return n*n`. Trace `square(3)`, `square(7)`, `square(10)`.
Example 11
easyTrue or false: parameters are values that exist at the call site, while arguments live in the function definition.
Example 12
medium`def f(a, b, c=10): return a + b + c`. What is `f(1, 2, 5)`?
Example 13
easy`def f(a, b=5): return a + b`. What is `f(2)`?
Example 14
hardTrace: `def f(n, acc=0): if n == 0: return acc; return f(n-1, acc+n)`. What is `f(4)`?
Example 15
easyWhat is the output of the following? FUNCTION greet(name): OUTPUT 'Hello, ' + name. greet('Alice'). greet('Bob').
Example 16
mediumIn `def f(**kwargs): return kwargs`, called as `f(a=1, b=2)`, what type is `kwargs`?
Example 17
medium`def f(*nums): return sum(nums)`. What is `f(1, 2, 3, 4)`?
Example 18
medium`def f(a, b): a = a + 1; return a + b`. After `x = 5; f(x, 1)`, what is `x`?
Example 19
easy`def greet(name): return 'Hi ' + name`. What does `greet('Sam')` return?
Example 20
challengeDesign 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)`.