Practice: Function Parameters
Q1. Greet by name
Write a function greet(name) that prints Hello, followed by the name, then call it with "Maya".
Goal: Define a function with one parameter.
Q2. Sum of two
Write a function add(a, b) that returns the sum, and print add(7, 8).
Goal: Use two parameters in a function.
Q3. Average of three
Write average(x, y, z) that returns the average of three numbers. Print average(2, 4, 9).
Goal: Use several parameters in a calculation.
Q4. Rectangle perimeter
Write perimeter(length, width) returning the perimeter. Print the result for 6 and 4.
Goal: Apply parameters to a formula.
Q5. Repeat text
Write repeat(text, times) that returns the text repeated times times. Print repeat("ab", 3).
Goal: Use parameters of different types.
Q6. Largest of three
Write biggest(a, b, c) that returns the largest value without using max. Print biggest(4, 11, 7).
Goal: Combine parameters with conditions.
Q7. Count items
Write count_over(numbers, limit) that returns how many numbers are greater than limit. Print count_over([3, 8, 12, 5, 20], 7).
Goal: Pass a list as a parameter.
Q8. Basic call
What does this print?
def show(a, b):
print(a - b)
show(10, 4)Goal: Match arguments to parameters.
Q9. Swapped order
What does this print?
def show(a, b):
print(a - b)
show(4, 10)Goal: See that order changes the result.
Q10. String parameters
What does this print?
def join(first, last):
return first + " " + last
print(join("Ada", "Lovelace"))Goal: Use string parameters.
Q11. Rebinding a number
What does this print?
def bump(n):
n = n + 1
return n
x = 5
bump(x)
print(x)Goal: Understand that rebinding is local.
Q12. Changing a list
What does this print?
def add_item(items):
items.append("z")
box = ["a"]
add_item(box)
print(box)Goal: Know that mutable arguments change in place.
Q13. Rebinding a list
What does this print?
def reset(items):
items = []
items.append(1)
box = [5, 6]
reset(box)
print(box)Goal: Separate rebinding from mutation.
Q14. Mixed parameters
What does this print?
def info(a, b=2, *rest, flag=False):
print(a, b, rest, flag)
info(1, 5, 7, 8, flag=True)Goal: Read a mixed parameter list.
Q15. Fix the call
This code crashes with a TypeError. Fix the call so it prints 12.
def multiply(a, b):
return a * b
print(multiply(3))Goal: Fix a missing argument.
Q16. Fix the definition
This code has a SyntaxError. Fix the function definition so it prints 7.
def power(base=2, exp):
return base ** exp
print(power(2, 3) - 1)Goal: Order default parameters correctly.
Q17. Fix the missing return
This function should return the square but the program prints None. Fix it so it prints 49.
def square(n):
n * n
print(square(7))Goal: Return a computed value.
Q18. Parameter or argument?
In def area(r): and the call area(5), which is the parameter and which is the argument? Explain briefly.
Goal: Define parameter and argument.
Q19. Why defaults go last
Explain in one or two sentences why a parameter with a default cannot come before a parameter without one.
Goal: Explain the ordering rule.
Q20. Choosing parameters
You write a function that sends an email. Name three sensible parameters and why.
Goal: Choose meaningful parameters.