Tutorial 13/15
REUSABLE CODE
Python: Params & Returns
Sending data into functions and getting results back.
The Hook
A function is like a Vending Machine.
- Input (Parameters): You feed in money and a code.
- Process: The machine checks the price and finds the snack.
- Output (Return): The actual snack falls into the tray!
Think about the flow: Input -> Machine -> Output.
Worked Example
A machine that adds two numbers and returns the result.
# Inputs go in brackets (a, b)
def add_machine(a, b):
# Result drops out
return a + b
# Use it like local variable
result = add_machine(5, 10)
print(result)
Logic Gate
Predict Output
What snack does this machine drop?
def magic(x):
return x * x
print(magic(5))
return x * x
print(magic(5))
Level 34: Bronze
Username Creator
"Modify the function to return first + last combined."
> bronze_stream_standby
Level 35: Silver
Broken Calculator
"The function expects two inputs but only one is given. Fix the call at the bottom."
> debugger_module_monitoring
Level 36: Gold
Area Factory
Independent Search Pattern
- Define function
calculate_area(w, h). - Inside,
return w * h. - Print the area of a 5x5 square (Expected: 25).
> computation_vector_online