Reading — step 1 of 8
Learn
Imagine writing a 500-line program with the same 5-line calculation repeated in five different places. When you find a bug, you have to fix it five times — and miss one. Functions solve this. A function is a named, reusable block of code. It's the single most important organizational tool in programming.
Three reasons to write a function: reuse (call it many times), abstraction (give a meaningful name to a multi-step operation), and isolation (variables inside the function don't leak out).
The shape of a function
Four pieces:
- The keyword
def. - The function name (snake_case, descriptive).
- Parameters in parentheses — these are local variable names that will receive values when the function is called.
- A colon, an indented body, and (usually) a
returnstatement.
To USE the function, you call it:
When greet("Alice") runs, Python sets the local variable name = "Alice", runs the body, and the result of return is the value of the call.
Parameters vs arguments
- Parameter — the name in the function definition (
nameindef greet(name)). - Argument — the actual value passed when calling (
"Alice"ingreet("Alice")).
Not critical to memorize, but the distinction comes up in error messages.
return — sending a value back
return does TWO things at once: it sets the function's output value AND it ends the function immediately. Anything after return is unreachable.
Without an explicit return, the function returns None:
This trips up beginners — they expect a return value because something happened, but only print is output; return is the function's value.
Multiple parameters and arguments
Default values
Provide a default for a parameter:
Defaults must come AFTER parameters without defaults: def f(a, b=10) ✓, def f(a=10, b) ✗ SyntaxError.
Docstrings — document what the function does
A string literal as the first line of a function body is a docstring — Python treats it specially:
Docstrings show up in IDE tooltips, help() output, and Sphinx docs. Write them for any function someone other than you might use.
The function as a black box
The ideal function is a black box: you know what goes in, what comes out, and you don't need to read the body to use it.
If you find yourself reading the body of a function every time you call it, the function name is bad. process_data(x) tells you nothing. parse_log_line(x) does.
Variables inside functions are LOCAL
This is a feature, not a bug — it prevents accidental clashes. If two functions both use a variable called i for a loop, they don't interfere.
Common mistakes
- Forgetting
return— function silently returns None and the caller gets nothing useful. defwithout():def greet name:is a SyntaxError.- Defining without calling:
def greet(): print("hi")doesn't print until you actually callgreet(). Common confusion for new programmers. - Calling without parentheses:
print(greet)prints<function greet at 0x...>— that's the function object itself, not the result. You meantprint(greet()). - Confusing
printwithreturn: a function that only prints can't be composed (x + greet("hi")makes no sense ifgreetreturns None).
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…