──────────────────────────────────────

13. Festival Workshop Shortlist · Use Defaults and Local Names

Call a function with its default and with a named override, then trace its local values.

Lesson files · lessons/callables/scope-and-defaults

Before you start
- Define a function with parameters and a return value.
- Trace an assignment and a printed result line by line.

By the end
- Call the same function with its default and with a named override, including zero.
- Trace the value used by each call and the value of a same-named variable outside the function.

Where you can use it
Use the usual setup time for most events and pass a different rate by name for one event.

In Foundations you passed values to a function and used its return value. A positional argument matches a parameter by order. A keyword argument names the parameter. A default supplies a value when the call leaves the argument out. Python sets the default when it defines the function. Use a number or a string for these examples.

Trace the two calls to estimate_time. The first uses the default slot_length of 15. The second uses 10. On each call, Python multiplies that call’s values and stores the result in a local name, total. The last print reads slot_length outside the function, where it was set to 60.

Predict
What does the final print show?
- 60
- 10

Worked example (example.py)

slot_length = 60


def estimate_time(rounds, slot_length=15):
    total = rounds * slot_length
    return total


print(estimate_time(3))
print(estimate_time(3, slot_length=10))
print(slot_length)


Example result
45
30
60

Complete prep_time(groups, minutes=12). Calculate groups times minutes in a local variable and return it. The input supplies the number of groups and a rate override on separate lines. Print the default total first. Use a keyword argument to print the total at the supplied rate on the next line.

Starter program (starter.py)

def prep_time(groups, minutes=12):
    return 0


groups = int(input())
rate = int(input())
print(prep_time(groups))
print(prep_time(groups))


Starter input
3
5

Hint
Keep the first call as prep_time(groups) and use minutes=rate in the second call.

Case 1. Try the sample
Input
3
5
Expected output
36
15

Case 2. No minutes per group
Input
4
0
Expected output
48
0

Case 3. No groups
Input
0
7
Expected output
0
0

Stretch
Add a third call that passes both arguments by name in the opposite order.

Reflect
For the sample and the zero-rate case, what value does minutes take in each call?

Check your prediction
60 The named argument sets the local parameter for that call. The name outside the function still has the value 60.
