Concepts / Function Parameters and Return Values

Function Parameters and Return Values

Parameters are specified within the parentheses in the function definition, separated by commas. When you call the function, you supply the values in the same way — in the same order, separated by commas.

  • Programming
Interactive lab

Try it: Defining and Calling Functions

What def and a call really do: def only creates a function object, a call binds the arguments to the parameters in a new frame and runs the body, return ends the call at once and its value replaces the call expression in the caller — while a function that only prints gives back None.

How it works

  1. def name(params): creates a function object and binds the name to it; the body does not run yet.
  2. A call evaluates its arguments, opens a new frame, binds each parameter to its argument and runs the body from the top.
  3. return value ends the call immediately (lines after it never run) and sends the value back; the frame and its local names are discarded.
  4. In the caller, the call expression is replaced by the returned value, which can be stored, used in a larger expression or passed to another call.
  5. A body that ends without return (e.g. one that only prints) makes the call evaluate to None — so x = f() stores None and None + 1 raises TypeError.
  6. Calling a name before its def has run raises NameError: the function does not exist yet.

Default run (18 steps): The program is ready. Python runs it from the top, one statement at a time. Idea: the value a call returns is what the caller stores. … The program finished. Console: 8.

Simplified: Four fixed, hand-written programs; you edit the literal arguments, switch the body between return and print, and move the def after the calls. The lab's own small evaluator runs them (your input is never executed as code); every variant was run in CPython 3.12 and the lab reproduces its sys.settrace line/call/return/exception events, its output and its error message. Functions are shown as <function name> without a memory address.

Educational simulation

Loading the simulation…

What Parameters Do

A function without parameters always does the same thing every time you call it. A function with parameters lets you pass in different values so the function can work with different inputs and produce different results. Parameters are input to the function — they are variables that receive values when you call the function. Think of a parameter as a placeholder that gets filled in with a real value (called an argument) at call time.

What do you think happens?

If you define a function say_hello() with no parameters, and call it three times, what happens?

  • It prints a different greeting each time

  • It prints the same greeting all three times

  • It asks you for input each time

Reveal answer
Answer:

It prints the same greeting all three times

Without parameters, the function has no way to receive different inputs. It will execute the same code and produce the same output every call. Parameters are what allow a function to behave differently based on what you pass in.

Parameters vs. Arguments

The terminology matters: parameters are the names you write in the function definition (inside the parentheses after the function name). Arguments are the actual values you supply when you call the function. When you call a function with arguments, each argument is assigned to the corresponding parameter in order.

Key point

Parameters are specified within the parentheses in the function definition, separated by commas. When you call the function, you supply the values in the same way — in the same order, separated by commas.

Function Callprint_max(5, 8)Argument 15Parameter a5Argument 28Parameter b8first valuesecond valueassigned toassigned to
When you call a function with arguments, each argument is assigned to the parameter in the same position. What value goes into which parameter?

How Data Flows Through a Function

When you call a function, three things happen in sequence: first, the arguments you provide are matched to the parameters in order; second, the function body executes using those parameter values; third, if the function has a return statement, the value after return is sent back to the caller. The caller can then use that returned value (assign it to a variable, print it, use it in a calculation, etc.).

Caller CodeArguments5, 8Parameters a, ba=5, b=8Return Value8Caller receivesresultFunction BodyCompute maxpassesbecomeused inproducessent to
What data enters the function, gets processed, and what comes back out?

A Worked Example: Finding the Maximum

Worked example

Calling a function with two parameters

Define a function print_max that takes two parameters a and b, compares them, and prints the larger one. Then call it with the numbers 5 and 8.

  1. 1
    Define the function

    Write the function definition with two parameters a and b. Inside, use an if statement to compare them and print the larger value.

  2. 2
    Call with direct values

    Call print_max(5, 8). The value 5 is assigned to parameter a, and 8 is assigned to parameter b. The function compares them and prints 8.

  3. 3
    Call with variables

    Create variables x = 3 and y = 7, then call print_max(x, y). Now the value of x (3) is assigned to a, and the value of y (7) is assigned to b. The function compares them and prints 7.

  4. 4
    Observe the pattern

    The function works the same way both times because the mechanism is identical: arguments are assigned to parameters in order, the function body executes, and the result is produced. The source of the argument values (literal numbers or variables) does not matter.

Result

The function print_max successfully compares two inputs and prints the maximum, regardless of whether those inputs come from literal values or variables.

python

Return Values: Sending Data Back

A return statement sends a value back from the function to the code that called it. The caller can then capture that returned value by assigning it to a variable, printing it, or using it in an expression. Without a return statement, a function still executes but sends back nothing (or technically, None in Python).

Key point

Parameters are how data gets into a function. Return values are how data gets out. Together, they allow a function to receive input, process it, and send back a result.

python
Output (expected)
8
40

Parameters as Local Variables

Parameters are just like variables except that their values are assigned when the function is called, not by an assignment statement inside the function. They exist only during the function's execution. Once the function returns, the parameters cease to exist — they are local to that function call. This is called scope: parameters have local scope, meaning they are only accessible inside the function.

Caller Scopex5Function Scope(during execution)a5b3After functionreturnsx5a, bno longer exist
Where do parameters live during function execution, and what happens to them after?
python

Common Mistakes with Parameters and Return Values

  • Common mistake

    Forgetting to match the number of arguments to the number of parameters

    What it looks like
    def add(a, b):
        return a + b
    add(5)  # Only one argument, but function expects two
    Why it fails

    Python cannot assign the second argument to parameter b if you do not provide it. This causes a TypeError.

    Do this instead

    Always provide the same number of arguments as there are parameters: add(5, 3)

  • Common mistake

    Confusing the order of arguments

    What it looks like
    def subtract(a, b):
        return a - b
    subtract(3, 5)  # Returns -2, not 2
    Why it fails

    Arguments are assigned to parameters in order. The first argument goes to the first parameter, the second to the second. If you reverse them, you get a different result.

    Do this instead

    Pay attention to the order: subtract(5, 3) returns 2

  • Common mistake

    Trying to use a parameter outside the function

    What it looks like
    def multiply(x, y):
        return x * y
    result = multiply(4, 5)
    print(x)  # NameError: x is not defined
    Why it fails

    Parameters have local scope. They only exist inside the function. Once the function returns, they are gone.

    Do this instead

    Use the returned value instead: print(result)

  • Common mistake

    Forgetting to return a value when the function should produce one

    What it looks like
    def square(n):
        n * n  # Missing 'return'
    Why it fails

    Without a return statement, the function computes the value but does not send it back to the caller. The caller receives None instead.

    Do this instead

    Add the return statement: return n * n

  • Common mistake

    Assuming a function modifies the original variable passed as an argument

    What it looks like
    def increment(x):
        x = x + 1
        return x
    num = 5
    increment(num)
    print(num)  # Still 5, not 6
    Why it fails

    The parameter x inside the function is a local variable. Changing it does not affect the original variable num in the caller's scope.

    Do this instead

    Capture the return value: num = increment(num)

Parameters and Arguments in Practice

Best practice

Use meaningful parameter names that describe what the parameter represents. For example, use calculate_total(price, tax_rate) instead of calculate_total(a, b). This makes the function easier to understand and use correctly. When you call the function, make sure you understand what each parameter expects and pass arguments in the correct order.

Best practice

If a function computes a result that the caller needs, always return it. Do not rely on printing or side effects alone. This makes the function reusable in different contexts — the caller can print the result, use it in a calculation, or pass it to another function.

Best practice

Test your function with different argument values to make sure it works correctly. This helps you catch mistakes early and ensures the function behaves as expected with various inputs.

Practice: Write and Call a Function

MEDIUM

Write a function called convert_to_celsius that takes one parameter fahrenheit (a temperature in Fahrenheit) and returns the equivalent temperature in Celsius. The formula is: Celsius = (Fahrenheit - 32) * 5/9. Then call your function with the values 32, 68, and 212 and print the results.

Hints
  • Define the function with one parameter named fahrenheit
  • Inside the function, apply the formula and return the result
  • Call the function three times with different argument values
  • Capture each return value and print it

Key Takeaways

  • Parameters are input variables in a function definition; arguments are the actual values you supply when calling the function.
  • Arguments are assigned to parameters in order: the first argument goes to the first parameter, the second to the second, and so on.
  • Parameters are local to the function — they exist only during execution and cannot be accessed outside the function.
  • A return statement sends a value back from the function to the caller, allowing the function to produce output.
  • Together, parameters and return values allow functions to receive input, process it, and send back results, making them flexible and reusable.

Related concepts

More concepts starting with F