Parameters and Arguments: What's the Difference?
A function is a named sequence of statements that performs a computation. You call it by name to execute its statements.
From Call to Result
A function is a named sequence of statements that performs a computation. Calling the function by its name causes those statements to execute. A function call connects the code that asks for the computation, the input data supplied to it, and the result produced afterward.
The three key parts of a function call are the function name, the argument or arguments supplied as input data, and the return value produced as the result.
A Function in Motion
Imagine a generated example with a function named double. Its statements take an input value, multiply that value by two, and produce the result. If the caller supplies 6, the function uses 6 during its computation and sends 12 back. The caller can then display 12, store it, or use it in another computation.
Tracing a Generated Function Call
A function named double multiplies its input by two. What result comes back when the caller supplies 6?
Find the function name: The function being called is double.
Find the argument: The caller supplies 6. That supplied value is the argument.
Follow the computation: The function's statements multiply the supplied value by two.
Identify the return value: Multiplying 6 by two produces 12, which is the result sent back to the caller.
The return value is 12.
Parameters Versus Arguments
A parameter is the function's named place for receiving input. An argument is the value or variable supplied by the caller in a particular function call. The parameter belongs to the function's definition; the argument belongs to the call that provides data to the function. In a generated example, a function may use the parameter number, while a caller supplies the argument 6. The parameter and argument correspond during that call, but they are not the same role.
| Term | Where it appears | Role |
|---|---|---|
| Parameter | In the function | Named place that receives input |
| Argument | In the function call | Value or variable passed into the function |
| Return value | After the function performs its work | Result sent back to the caller |
The terms describe different parts of the same data flow.
The Call Sequence
To analyze a function call, follow its sequence. First, the caller identifies the function by name. Next, the caller supplies one or more arguments as input data. The function's named statements then perform their computation using that input. Finally, the function produces a return value and sends it back to the caller. The returned value can be displayed, stored, or used in further computations.
Mistakes Beginners Make
Calling the argument a parameter
The supplied value is an argument. A parameter is the function's named place for receiving input.
Fix:
Use parameter for the function-side input name and argument for the value or variable supplied by the caller.Treating the function name as the result
The function name identifies the named sequence of statements to execute. The return value is the result produced after those statements perform their work.
Fix:
Follow the function's computation and identify the value sent back as the return value.Stopping after identifying the input
The argument is input data. The function uses it to perform its computation, which may produce a different return value.
Fix:
Trace the statements inside the function before predicting the returned result.Assuming a return value has no use
A return value is sent back to the caller and can be displayed, stored, or used in further computations.
Fix:
Look for how the caller uses the returned result.
When reading an unfamiliar function call, label four things in order: the function name, the supplied argument or arguments, the statements that use the input, and the return value. This prevents the common mistake of treating the input and output as the same thing.
Check Your Understanding
What do you think happens?
A generated function named add_three receives an argument and adds 3 to it. What return value should come back when the caller supplies 4?
Reveal answer
Answer: 7
The supplied value 4 is the argument. The function's statements add 3, so the return value is 7.
Consider a generated function named triple whose statements multiply its input by three. Identify the function name, the parameter's role, the argument in a call that supplies 5, and the return value.
Hints
- The function name identifies which named sequence of statements runs.
- The argument is the value supplied by the caller.
- Apply the function's stated computation to the supplied value.
Key Takeaways
- A function is a named sequence of statements that performs a computation when called.
- A function call includes a function name, argument or arguments, and a return value.
- A parameter is the function-side name or place for input; an argument is the value or variable supplied by the caller.
- Arguments flow into a function, and the function's computation produces a return value for the caller.
- A returned value can be displayed, stored, or used in further computations.
Key Takeaways
- A function is a named sequence of statements that performs a computation.
- The function name identifies what is called, while the argument supplies input data.
- The parameter is the function's receiving name or place for input; the argument is the supplied value or variable.
- The function uses its input, performs its statements, and sends a return value back to the caller.
- To predict a call's result, trace the argument through the function's computation.