A function is a named, reusable block of code that takes inputs through parameters, does one job, and usually hands a result back with a return statement. Scope is the companion rule: it decides which variables a function can see and which ones stay private to it.

If you only need the core idea, it is this: a function is a contract — values go in through the parameter list, exactly one result comes out through return, and everything created inside disappears when the call ends.

What a function is

The programming idea mirrors the math idea. In math, f(x)=2x+3f(x) = 2x + 3 maps an input to an output: f(4)=11f(4) = 11, every time. A programming function does the same, except it can also do things along the way — print, write files, update state. The closer a function stays to the pure math model (same inputs, same output, no side effects), the easier it is to test and reuse.

Defining a function does not run it. The body executes only when the function is called.

def area(width, height):      # definition — nothing runs yet
    return width * height

a = area(3, 4)                # call — body runs, a becomes 12

Parameters vs. arguments

These two words are often mixed up, but they name different things:

Term What it is Where it appears Example
Parameter a named placeholder the function definition width, height
Argument an actual value the function call 3, 4

When area(3, 4) runs, arguments are matched to parameters by position: width = 3, height = 4. Swap the call to area(4, 3) and each parameter gets a different value — harmless for multiplication, disastrous for subtract(a, b). Many languages also allow keyword arguments (area(height=4, width=3)) and default values (def greet(name="friend")) to make calls explicit.

What return actually does

A return statement does two things at once:

  1. It immediately ends the function call — no code after it runs.
  2. It sends one value back to wherever the call happened.

A function with no return (or a bare return) gives back a "nothing" value — None in Python, undefined in JavaScript. And printing is not returning: print shows a value on the screen, while return delivers it to the calling code so it can be stored, compared, or passed onward.

Worked example 1: tracing a call step by step

Compound interest is computed by the formula

A=P(1+r)tA = P\,(1 + r)^t

As a function:

def compound(p, r, t):
    return p * (1 + r) ** t

Trace compound(1000, 0.05, 2):

  1. Arguments bind to parameters: p = 1000, r = 0.05, t = 2.
  2. The body evaluates 1000(1.05)2=10001.10251000 \cdot (1.05)^2 = 1000 \cdot 1.1025.
  3. return sends back 1102.5, and the call expression compound(1000, 0.05, 2) is replaced by that value.

The caller never sees p, r, or t — those names existed only for the duration of the call.

Scope: where variables live

Scope answers one question: from where can this name be seen?

  • A variable created inside a function (including every parameter) is local — it exists only during that call.
  • A variable created at the top level of a file is global — readable from inside functions, but reassigning it inside a function usually creates a new local instead.

When the same name exists in both places, the local one wins inside the function. That is called shadowing.

Worked example 2: a scope trace

Predict the output before reading the answer:

total = 10

def add(n):
    total = n + 5     # local total — shadows the global one
    return total

result = add(3)
print(result)         # ?
print(total)          # ?

Step by step: the call add(3) binds n = 3, creates a local total = 8, and returns 8. So result is 8 — but the global total is still 10, because the assignment inside add created a brand-new local variable instead of touching the global. When the call ended, that local total was destroyed.

This single example explains a large fraction of beginner scope bugs: assignment inside a function does not reach outward.

Common function mistakes

Printing instead of returning

print(result) inside the function shows the value once, but the caller receives nothing. If you ever write x = my_function(...) and x is empty, check for a missing return.

Code placed after return

Anything below a return in the same path is dead code — it can never execute.

Wrong argument order

Positional arguments are matched silently. divide(2, 10) and divide(10, 2) both run without error and return different answers. Keyword arguments remove the ambiguity.

Expecting a local change to update a global

As the scope trace shows, reassigning a global name inside a function creates a local shadow. If a function must report a new value, return it and reassign at the call site.

Why functions are worth the ceremony

Functions are the unit of decomposition: a large problem becomes small named pieces that can be tested one at a time and reused everywhere. They are also the foundation of bigger ideas — a recursive algorithm is just a function calling itself with a smaller input, and methods in object-oriented programming are functions attached to objects.

One to try on your own

Convert Celsius to Fahrenheit with F=95C+32F = \tfrac{9}{5}C + 32. Write to_fahrenheit(c), then predict the return values of to_fahrenheit(0) and to_fahrenheit(100) before running it (they should be 32 and 212). If your prediction and the actual results agree, you have the parameter–return contract down.

Frequently Asked Questions

What is the difference between a parameter and an argument?
A parameter is the named placeholder in a function definition, while an argument is the actual value supplied when the function is called. In a call, arguments are matched to parameters, usually by position. So in a definition like area(width, height), width is a parameter, and in the call area(3, 4), the number 3 is an argument.
What happens if a function has no return statement?
The function still runs, but it sends back a nothing value: None in Python, undefined in JavaScript. Any variable assigned from the call will hold that empty value. This is the classic symptom of printing a result inside the function instead of returning it, so check for a missing return whenever a call seems to produce nothing.
What is variable scope in a function?
Scope is the set of places in a program where a variable name can be seen. Variables created inside a function, including its parameters, are local: they exist only during that call and disappear afterward. Variables defined at the top level are global and can be read inside functions, but reassigning the name inside a function normally creates a new local instead.
Can a function call itself?
Yes. A function that calls itself is recursive, and it needs a base case that stops the chain of calls plus a step that moves each call toward that base case. Each call gets its own fresh set of local variables, which is exactly what makes recursion work. Factorial and tree traversal are standard examples.
Why use functions instead of writing all the code in one place?
Functions break a large problem into small named pieces that can be written, tested, and fixed independently. They remove duplication, because one definition serves many calls, and they make code readable, since a good name summarizes what a block does. They are also the basis for recursion and for methods in object-oriented programming.

Need help with a problem?

Upload your question and get a verified, step-by-step solution in seconds.

Open GPAI Solver →