Concepts / Parameters and Arguments: The Basics

Parameters and Arguments: The Basics

Expressions and variables can both be passed as arguments to user-defined functions, just as they can be passed to built-in functions.

  • Programming

Before the Function Runs

When you call a function, the information inside the parentheses is prepared before the function begins executing. That information may be a literal value, a variable, or a calculation. Python evaluates the argument first, then gives the resulting value to the function.

What do you think happens?

Which happens first when a function call contains an expression: the expression is evaluated, or the function begins executing?

  • The expression is evaluated first

  • The function begins executing first

Reveal answer
Answer:

The expression is evaluated first

Python computes the argument before the function is called. The function receives the resulting value, not the unevaluated expression.

Function callprint_twice(expression)Expressioncomputed onceResult valuepassed to functionFunction executionparameter receives valuestartsproducessupplies
What happens first when a function call contains an expression?

Expressions as Arguments

An expression is a combination of values, operators, and function calls that Python can evaluate to produce a result. When an expression is used as an argument, the expression itself does not reach the function. Python computes it first, and only the resulting value is passed to the function.

Worked example

Passing a Repeated String

Determine what value print_twice receives when it is called with the expression 'Spam ' * 4.

  1. 1
    Evaluate the expression

    Python computes 'Spam ' * 4 once, producing the string 'Spam Spam Spam Spam '.

  2. 2
    Pass the result

    The resulting string is passed to print_twice as its argument.

  3. 3
    Bind the parameter

    Inside print_twice, the parameter bruce holds the resulting string.

  4. 4
    Use the parameter

    The function prints the value held by bruce twice.

Result

The string repetition happens once before the function runs. The function receives one string value and prints that value twice.

'Spam ' * 4expression'Spam Spam Spam Spam'computed valuebruceparameterevaluates topasses value
How does the value produced by an expression move into a function parameter?
Worked example

An Expression That Calls Another Function

Determine what print_twice receives when its argument is math.cos(math.pi).

  1. 1
    Evaluate the nested expression

    Python evaluates math.cos(math.pi) before calling print_twice.

  2. 2
    Identify the result

    The expression produces the value -1.0.

  3. 3
    Pass one value

    The value -1.0 is passed to print_twice.

  4. 4
    Count the function calls

    math.cos() is called once during argument evaluation, even though print_twice prints the received value twice.

Result

print_twice receives -1.0, and the cosine calculation occurs once before print_twice executes.

Variables as Arguments

A variable holds a value. When you pass a variable as an argument, Python looks up the value stored in that variable and passes the value to the function. The variable name itself is not passed.

Worked example

Passing a Stored String

Determine what print_twice receives when michael stores the string 'Eric, the half a bee.' and the call is print_twice(michael).

  1. 1
    Look up the variable

    Python retrieves the value stored in michael.

  2. 2
    Pass the value

    The stored string, 'Eric, the half a bee.', is passed to print_twice.

  3. 3
    Bind the parameter

    Inside print_twice, the parameter bruce receives the string value, not the name michael.

  4. 4
    Use the parameter

    The function prints the value held by bruce twice.

Result

The function receives 'Eric, the half a bee.' and prints that value twice.

Expression argument'Spam ' * 4Result value'Spam Spam Spam Spam 'Variable argumentmichaelStored value'Eric, the half a bee.'evaluates toretrieves
What value does the parameter receive when the argument is an expression versus a variable?

Predicting the Result

Argumentexpression or variablePrepared valueevaluated onceParameterholds received valuePrinted valuefunction uses parameterevaluate or retrievepassuse
Given an expression or variable as an argument, what value reaches the parameter and what output follows?

What do you think happens?

A function prints its parameter twice. If its argument is 'Spam ' * 4, how many times is the string repetition performed?

  • Once, before the function is called

  • Twice, because the function prints twice

  • Four times, because the expression contains 4

Reveal answer
Answer:

Once, before the function is called

The expression is evaluated once to create one string value. The function then uses that received value twice.

Worked example

A Reliable Prediction Method

Predict the behavior of a function call that passes either an expression or a variable.

  1. 1
    Inspect the argument

    Decide whether the argument is an expression that must be computed or a variable whose stored value must be retrieved.

  2. 2
    Determine the value

    For an expression, calculate its result. For a variable, identify the value currently stored in it.

  3. 3
    Transfer the value

    Treat that result as the single value received by the function's parameter.

  4. 4
    Follow the function body

    Predict the output from the parameter value, remembering that the function does not receive the original expression or variable name.

Result

The key prediction is always the value produced or retrieved before the function begins executing.

Debugging Unexpected Values

Tracing argument evaluation helps separate problems in preparing an argument from problems inside the function. First identify whether the argument was an expression or a variable. Then determine the value produced by the expression or stored in the variable. Finally check how the function uses the parameter.

Argument valuebefore callParameter valueinside functionFunction outputobserved resultChecked argumentexpression or variablevalueChecked parameterreceived valueExplained outputfunction behaviortrace valuetrace use
When a function produces an unexpected result, which stage should you inspect?
  • Common mistake

    Assuming the function receives the expression itself

    What it looks like

    Treating 'Spam ' * 4 as though the function receives an unevaluated instruction

    Why it fails

    Python computes the expression before the function is called.

    Do this instead

    Replace the expression mentally with its result, then trace that value into the parameter.

  • Common mistake

    Assuming a variable name is passed

    What it looks like

    Reasoning that print_twice(michael) gives the function the name michael

    Why it fails

    Python retrieves the value stored in michael and passes that value.

    Do this instead

    Inspect the value stored in the variable before tracing the function call.

  • Common mistake

    Counting repeated function use as repeated argument evaluation

    What it looks like

    Assuming the string repetition in 'Spam ' * 4 happens twice because the function prints twice

    Why it fails

    The argument is evaluated once before the function executes.

    Do this instead

    Separate the one-time preparation of the argument from the later uses of the parameter.

  • Common mistake

    Debugging only the function body

    What it looks like

    Looking inside the function without checking the expression result or variable value

    Why it fails

    An unexpected value may have been prepared before the function started.

    Do this instead

    Trace the argument first, then the parameter, and finally the function's use of that parameter.

Practice and Takeaways

MEDIUM

For each function call, identify the value that reaches the parameter before the function starts: a call using 'Spam ' * 4, a call using math.cos(math.pi), and a call using the variable michael that stores 'Eric, the half a bee.'. Then explain why the original expression or variable name is not what the parameter receives.

Hints
  • Evaluate an expression before tracing the function.
  • For a variable argument, look up the value stored in the variable.
  • The parameter receives the resulting value.
  1. 1
    User-defined functions accept expressions and variables as arguments, just as built-in functions do. Python evaluates an argument exactly once before calling the function. An expression contributes its computed result, while a variable contributes the value stored in it. To predict output or debug an unexpected result, trace the value from the argument, to the parameter, and then through the function's execution.

At a glance

  • Expressions and variables can both be passed to user-defined functions.
  • Arguments are evaluated exactly once before the function is called.
  • An expression contributes its result; a variable contributes its stored value.
  • The parameter receives a value, not the original expression or variable name.
  • Tracing argument value, parameter value, and function use helps debug unexpected behavior.

Related concepts

More concepts starting with P