Reading preferences
Concept

Functions and Scope

Name a reusable job, pass information to it, and return a result.

python, functions, parameters, arguments, return, scope

Name a reusable job

Imagine a recipe that doubles a number of portions. Rather than rewriting the steps every time, give the job a name and provide a number when needed. A function groups a job under a name. Define it with def, then call it using parentheses.

call double(4) → parameter number receives 4 → return 8 → caller uses 8

The parameter is the name in the function definition. An argument is the value supplied in a call. return ends the function and gives a value back to the caller. It is not the same as print, which displays a value.

Python illustration

def double(number):
    result = number * 2
    return result

answer = double(4)
print(answer)
print(double(0))

Prepared output — not live execution:

8
0

The body is indented. Defining the function does not execute its body; calling it does. If a function finishes without an explicit return value, it returns None. Each call gets its own local names; result in this example is not available as a local name in the caller.

See the reasoning

Follow the example one step at a time

Follow the call double(4) from argument to returned value. This is a prepared teaching demonstration, not live code execution.

Call the function

caller: double(4)

The argument 4 is supplied when the function is called.

Make the job clear

Choose a meaningful name, explain what inputs are expected, and test small cases such as zero. A function can return a value without displaying it, so another function can use the result. More elaborate argument forms and scope rules are planned for later; do not assume every name is local in every Python construct.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What is a parameter?
2. In double(4), what is 4?
3. What does return do?
4. Where is a name assigned inside a simple function normally local?

References and further reading