Multiple Parameters and Arguments
A parameter is a variable declared in a function's parentheses to receive input.
From Call to Return
A function definition describes a reusable block of code, but defining that function does not execute its body. Execution begins when the program encounters a function call. At that point, the program pauses at the call, transfers control to the function's code block, executes the statements inside it in order, and then returns to the line after the call.
Following One Invocation
A greeting function called twice
Imagine a function named greet that accepts one parameter named person. A program calls greet with the argument Ada, then later calls greet with the argument Lin.
- 1First call
Execution reaches the call with Ada. Control transfers to the function body, and the parameter person receives the argument value Ada.
- 2First execution
The function body runs using the value currently held by person. When the function finishes, control returns to the line after the first call.
- 3Second call
Execution later reaches the same function again with Lin. This starts another execution of the function body, with person receiving Lin for this call.
- 4Second return
After the second execution finishes, control returns to the line after the second call.
The same function body is executed once for Ada and once for Lin. Each call has its own argument value and its own call, execute, return cycle.
A repeated call is not a continuation of the previous function execution. Each call triggers a fresh execution of the function's code, after which control returns to the caller.
Parameters and Arguments
A parameter is a variable declared in a function's parentheses to receive input. An argument is the actual value supplied in a function call. Parameters describe the inputs a function can receive; arguments provide the values for a particular invocation.
| Term | Where it appears | Purpose |
|---|---|---|
| Parameter | Function definition | Names a variable that receives input |
| Argument | Function call | Provides the actual value for that invocation |
Parameters name receiving variables; arguments supply values during calls.
Matching Multiple Inputs
When a function declares multiple parameters, the arguments in a call are matched to those parameters in order. The first argument supplies the first parameter, the second argument supplies the second parameter, and so on. The parameters then act as local variables inside the function, holding the values supplied by that call.
Two inputs for one calculation
Imagine a function named combine that declares two parameters: first and second. One call supplies the arguments 12 and 5.
- 1Read the definition
The function declares two receiving variables, first and second.
- 2Match by position
The first supplied value, 12, is matched to first. The second supplied value, 5, is matched to second.
- 3Execute the body
The function body runs with first holding 12 and second holding 5.
- 4Return to the caller
When the function finishes, control returns to the line after the call.
The order of the arguments determines which value each parameter receives.
Count the parameters before calling a function, then provide the corresponding number of arguments in the intended order. Always include parentheses when calling a function.
Reuse Without Repetition
The main benefit of parameters becomes visible when one function is called from different places with different values. The function's code is written once, while each call supplies the inputs needed for that situation. This makes the program shorter and easier to maintain. If the function's logic needs a correction or improvement, the change is made in the function definition rather than in every repeated copy.
The function body does not need to be duplicated for each input. A new call supplies new arguments, starts a new execution, and uses the same function code with those values.
Mistakes with Calls
Common mistake Writing a function name without parentheses when intending to call it.
What it looks likeTreating the function name alone as the invocation.
Why it failsA function call requires parentheses.
Do this insteadUse the function name followed by parentheses, supplying arguments inside them when parameters are declared.
Common mistake Providing the wrong number of arguments.
What it looks likeCalling a function with two parameters while supplying only one argument.
Why it failsThe function's parameters require corresponding argument values.
Do this insteadMatch the number of supplied arguments to the number of parameters.
Common mistake Confusing a parameter with an argument.
What it looks likeCalling the supplied value a parameter.
Why it failsA parameter is declared by the function; an argument is supplied by the call.
Do this insteadUse parameter for the receiving name in the definition and argument for the actual value in the invocation.
Common mistake Assuming execution continues to the next caller line before the function body runs.
What it looks likeReading a function call as if it were skipped temporarily.
Why it failsControl transfers to the function body immediately when the call is encountered.
Do this insteadTrace the function body first, then continue at the line after the call when the function finishes.
Common mistake Copying the function body for every new input.
What it looks likeCreating several nearly identical blocks for different values.
Why it failsThe same function can be called multiple times with different arguments.
Do this insteadWrite the behavior once in the function and invoke it wherever that behavior is needed.
Trace the Next Call
Suppose a function named describe declares two parameters, item and place. A program calls it with the arguments notebook and desk. Describe which value each parameter receives, what happens immediately after the call is encountered, and where execution continues after the function finishes.
Hints
- Match arguments to parameters from left to right.
- Remember the call, execute, return cycle.
- The caller continues at the line after the function call.
Explain why calling one parameterized function three times can be preferable to writing the same function body three times.
Hints
- Consider code repetition.
- Consider what happens when the function logic needs to be fixed or improved.
Essential Takeaways
- 1A function call transfers execution to the function body and returns control to the caller when the function finishes.
- 2Parameters are receiving variables declared by a function; arguments are the actual values supplied by a call.
- 3With multiple parameters, arguments match parameters in order.
- 4Every invocation starts a fresh execution of the function body.
- 5Repeated calls reuse one function definition, reducing code repetition and simplifying maintenance.
At a glance
- A function call pauses the caller, executes the function body, and then returns to the line after the call.
- Parameters define the inputs a function can receive, while arguments provide the values for a particular call.
- Arguments are matched to multiple parameters in order.
- Calling one parameterized function repeatedly allows different inputs without duplicating the function's code.
- To trace a call, follow the sequence: identify the function, map the arguments, execute the body, and return to the caller.