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.
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?
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.
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.
Passing a Repeated String
Determine what value print_twice receives when it is called with the expression 'Spam ' * 4.
Evaluate the expression: Python computes 'Spam ' * 4 once, producing the string 'Spam Spam Spam Spam '.
Pass the result: The resulting string is passed to print_twice as its argument.
Bind the parameter: Inside print_twice, the parameter bruce holds the resulting string.
Use the parameter: The function prints the value held by bruce twice.
The string repetition happens once before the function runs. The function receives one string value and prints that value twice.
An Expression That Calls Another Function
Determine what print_twice receives when its argument is math.cos(math.pi).
Evaluate the nested expression: Python evaluates math.cos(math.pi) before calling print_twice.
Identify the result: The expression produces the value -1.0.
Pass one value: The value -1.0 is passed to print_twice.
Count the function calls: math.cos() is called once during argument evaluation, even though print_twice prints the received value twice.
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.
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).
Look up the variable: Python retrieves the value stored in michael.
Pass the value: The stored string, 'Eric, the half a bee.', is passed to print_twice.
Bind the parameter: Inside print_twice, the parameter bruce receives the string value, not the name michael.
Use the parameter: The function prints the value held by bruce twice.
The function receives 'Eric, the half a bee.' and prints that value twice.
Predicting the Result
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?
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.
A Reliable Prediction Method
Predict the behavior of a function call that passes either an expression or a variable.
Inspect the argument: Decide whether the argument is an expression that must be computed or a variable whose stored value must be retrieved.
Determine the value: For an expression, calculate its result. For a variable, identify the value currently stored in it.
Transfer the value: Treat that result as the single value received by the function's parameter.
Follow the function body: Predict the output from the parameter value, remembering that the function does not receive the original expression or variable name.
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.
Assuming the function receives the expression itself
Python computes the expression before the function is called.
Fix:
Replace the expression mentally with its result, then trace that value into the parameter.Assuming a variable name is passed
Python retrieves the value stored in michael and passes that value.
Fix:
Inspect the value stored in the variable before tracing the function call.Counting repeated function use as repeated argument evaluation
The argument is evaluated once before the function executes.
Fix:
Separate the one-time preparation of the argument from the later uses of the parameter.Debugging only the function body
An unexpected value may have been prepared before the function started.
Fix:
Trace the argument first, then the parameter, and finally the function's use of that parameter.
Practice and Takeaways
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.
- 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.
Key Takeaways
- 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.