Practice: Function Parameters

« Read the chapter

Q1. Greet by name

BeginnerCoding

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

BeginnerCoding

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

BeginnerCoding

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

BeginnerCoding

Write perimeter(length, width) returning the perimeter. Print the result for 6 and 4.

Goal: Apply parameters to a formula.

Q5. Repeat text

IntermediateCoding

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

IntermediateCoding

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

IntermediateCoding

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

BeginnerOutput

What does this print?

Code
def show(a, b):
    print(a - b)

show(10, 4)

Goal: Match arguments to parameters.

Q9. Swapped order

BeginnerOutput

What does this print?

Code
def show(a, b):
    print(a - b)

show(4, 10)

Goal: See that order changes the result.

Q10. String parameters

BeginnerOutput

What does this print?

Code
def join(first, last):
    return first + " " + last

print(join("Ada", "Lovelace"))

Goal: Use string parameters.

Q11. Rebinding a number

IntermediateOutput

What does this print?

Code
def bump(n):
    n = n + 1
    return n

x = 5
bump(x)
print(x)

Goal: Understand that rebinding is local.

Q12. Changing a list

IntermediateOutput

What does this print?

Code
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

AdvancedOutput

What does this print?

Code
def reset(items):
    items = []
    items.append(1)

box = [5, 6]
reset(box)
print(box)

Goal: Separate rebinding from mutation.

Q14. Mixed parameters

AdvancedOutput

What does this print?

Code
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

BeginnerDebugging

This code crashes with a TypeError. Fix the call so it prints 12.

Code
def multiply(a, b):
    return a * b

print(multiply(3))

Goal: Fix a missing argument.

Q16. Fix the definition

IntermediateDebugging

This code has a SyntaxError. Fix the function definition so it prints 7.

Code
def power(base=2, exp):
    return base ** exp

print(power(2, 3) - 1)

Goal: Order default parameters correctly.

Q17. Fix the missing return

BeginnerDebugging

This function should return the square but the program prints None. Fix it so it prints 49.

Code
def square(n):
    n * n

print(square(7))

Goal: Return a computed value.

Q18. Parameter or argument?

BeginnerConceptual

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

IntermediateConceptual

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

IntermediateApplication

You write a function that sends an email. Name three sensible parameters and why.

Goal: Choose meaningful parameters.