def, Parameters, and return
Scenario: The same health calculation appears in five scripts.
New words, in plain English
- Function - a named reusable block of code
- Parameter - an input name in a function definition
- Argument - the actual value supplied in a call
- return - send a result back to the caller
Define with def. A function call creates local parameter names. return ends the call; without an explicit return, Python returns None.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
def healthy(status_code):
return 200 <= status_code < 400
print(healthy(204))
Tip: Prefer functions that return useful values over functions that only print.
Goal: Practice this idea in the py-first-function challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.