Writing Functions to Reuse a Block of Code
Byte the robot stands at a glowing workbench covered in labeled recipe cards, holding a single card named 'greet' and pointing to a stack of identical steps that have been neatly folded away inside it
- Explain what a function is and why programmers use them to avoid repeating code
- Identify the parts of a function: name, parameter, and body
- Predict what output a function call will produce when given a specific input
- Explain why defining a function once is preferable to repeating the same steps throughout a program
Key terms
- Function
- A named package of steps a program can run on demand
- Parameter
- A named placeholder that receives a value when the function runs
- Argument
- The actual value supplied to a parameter at call time
- Function body
- The indented steps that run each time the function is called
- Calling a function
- Using a function's name to run its steps
Parameters Versus Arguments
A parameter and an argument are two sides of the same handoff, and mixing them up is common. The parameter is the named slot written inside the parentheses when you define the function, like 'name' in def greet(name). The argument is the real value you hand in when you call it, like the text "Aisha" in greet("Aisha"). Each call copies its argument into the parameter, so the same function produces a different result depending on what value you pass.
Write Once, Change Once
The deepest payoff of a function is not just saving keystrokes; it is that the logic lives in exactly one place. If a greeting must change from "Hello" to "Welcome," you edit the single function body and every call across the whole program updates at once. Repeating the same steps by hand means hunting down every copy and risking a missed one. Even code used only two or three times is usually worth a function, because clear naming and one source of truth make programs easier to read and safer to change.
Worked examples
Predict the output of square(4) where def square(n) prints n times n
- Call square(4), so the argument 4 is copied into the parameter n.
- The body runs n * n with n now equal to 4.
- Compute 4 * 4, which is 16.
- The body prints the result 16, not the text 'n * n'.
Answer: 16
Trace two calls to greet with different names
- First call greet("Aisha") copies "Aisha" into name.
- The body prints Hello, Aisha! using that value.
- Second call greet("Marcus") copies "Marcus" into name.
- The same body now prints Hello, Marcus! with the new value.
Answer: Hello, Aisha! then Hello, Marcus!
Activity
Drag the code blocks into the correct order to define a function called 'double' that takes a number and prints it multiplied by 2, then call it with the value 5
Practice
Define a function named triple that prints a number times three.
Predict what double(5) prints if double prints its parameter times two.
Common mistakes to avoid
- Functions are only worth it after ten usesEven two or three uses justify a function because it improves readability and easy changes.
- Parameters and arguments are the same thingA parameter is the placeholder in the definition, while an argument is the value passed at call time.
Check your understanding
A programmer writes these two lines: def square(n): print(n * n) square(4) What will the program print?
A student says: 'I only need to define a function if I plan to use those steps more than ten times.' Is this correct?
Which part of a function definition gives the function its reusable name?
Recap
A function packages steps under a name so you can call them instead of repeating code. Parameters are placeholders in the definition, arguments are the values passed at call time, and keeping logic in one place means a single edit updates every call.
Reflect
What repeated steps in a program you imagine would be cleaner as a function?