Calling Built-in Functions
Arguments belong to the call. Parameters belong to the function's definition and local execution.
From Call to Execution
When you call a function, you provide values for the function to use. Those values are arguments. The function receives them through parameters, which are variables inside the function. Understanding this handoff lets you trace what a function is working with before you consider what it does next.
Arguments belong to the call. Parameters belong to the function's definition and local execution.
Position Controls the Match
Arguments are assigned to parameters by position. The first argument goes to the first parameter, the second argument goes to the second parameter, and so on. The names do not change this positional order. To trace a call, line up the values from left to right with the parameters from left to right.
Tracing Two Values
Suppose a function has parameters named first and second, and it is called with the values red and blue in that order. Which value does each parameter receive?
- 1Match position one
The first argument is red. The first parameter, first, receives red.
- 2Match position two
The second argument is blue. The second parameter, second, receives blue.
- 3Inspect the function body
The function body uses the parameter names first and second to refer to the values received during this call.
first refers to red, and second refers to blue during this function execution.
Inside the Local Scope
After the call supplies its arguments, the function works with the corresponding parameter variables. Parameter names are local to the function, so they exist while that function is executing. The important tracing boundary is this: a value written at the call becomes available through a parameter name inside the function.
Applying the Same Rule
Predicting the Values Used
Imagine a function with parameters named item and quantity. A call supplies notebook first and three second. What values are available through the parameters while the function executes?
- 1Read the parameter order
The parameters are ordered as item first and quantity second.
- 2Read the argument order
The arguments are ordered as notebook first and three second.
- 3Map the positions
Notebook is assigned to item because both are first. Three is assigned to quantity because both are second.
- 4Predict the body inputs
Any operation in the function body that uses item receives notebook, and any operation that uses quantity receives three.
During this execution, item refers to notebook and quantity refers to three.
This positional rule applies to built-in functions as well as user-defined functions. The source examples identify built-in functions such as math.sin and math.pow as functions that require arguments. The same argument-and-parameter relationship is used when a function created by a programmer receives values.
When predicting a function's result, first write the parameters in order, then write the arguments in order beneath them. Only after making those matches should you trace what the function does with the received values.
Mistakes in Argument Mapping
Common mistake Treating the argument and parameter as the same thing
What it looks likeCalling a function with notebook and describing item as if it were the value written in the call.
Why it failsThe argument is the value passed during the call, while item is the parameter variable inside the function.
Do this insteadSay that notebook is the argument and item receives it as a parameter.
Common mistake Matching by meaning instead of position
What it looks likeAssuming the second argument should go to the parameter whose name seems most related to it.
Why it failsThe stated rule assigns arguments by position: first to first, second to second, and so on.
Do this insteadLine up the argument and parameter lists from left to right.
Common mistake Assuming parameter names exist everywhere
What it looks likeReferring to a function parameter as though it remains available after that function finishes executing.
Why it failsParameter names are local to the function and exist while the function is executing.
Do this insteadTreat the parameter as part of the function's local scope.
Mapping Practice
A function has three parameters in this order: source, destination, and count. It is called with the values folder_a, folder_b, and four in that order. Identify the argument assigned to each parameter, then state which names are local to the function while it executes.
Hints
- Write the parameters in positions one, two, and three.
- Place the arguments beneath them in the same order.
- The parameter names are local to the function during its execution.
Practice Check
Using the call described above, determine the values received by source, destination, and count.
- 1First position
source receives folder_a.
- 2Second position
destination receives folder_b.
- 3Third position
count receives four.
The local parameter mapping is source to folder_a, destination to folder_b, and count to four.
Key Takeaways
- 1Arguments are values supplied during a function call.
- 2Parameters are variables inside the function that receive those arguments.
- 3Arguments map to parameters by position, from first to first and onward.
- 4Parameter names are local to the function while it is executing.
- 5Built-in functions and user-defined functions use this same argument-to-parameter relationship.
At a glance
- An argument is a value passed in a call; a parameter is the receiving variable inside the function.
- The first argument maps to the first parameter, the second maps to the second, and so on.
- Parameter names belong to the function's local scope while it executes.
- Trace a call by aligning arguments and parameters by position before predicting what the function does.