Skip to content
Defining Functions
step 1/8

Reading — step 1 of 8

Learn

~4 min readFunctions

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

python

Four pieces:

  1. The keyword def.
  2. The function name (snake_case, descriptive).
  3. Parameters in parentheses — these are local variable names that will receive values when the function is called.
  4. A colon, an indented body, and (usually) a return statement.

To USE the function, you call it:

python

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 (name in def greet(name)).
  • Argument — the actual value passed when calling ("Alice" in greet("Alice")).

Not critical to memorize, but the distinction comes up in error messages.

return — sending a value back

python

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:

python

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

python

Default values

Provide a default for a parameter:

python

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:

python

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.

python

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

python

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.
  • def without (): def greet name: is a SyntaxError.
  • Defining without calling: def greet(): print("hi") doesn't print until you actually call greet(). 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 meant print(greet()).
  • Confusing print with return: a function that only prints can't be composed (x + greet("hi") makes no sense if greet returns None).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…