Concepts / Parameters and Arguments: What's the Difference?

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.

  • Programming

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.

suppliesentersproducesreturns toCallerrequests a computationArgumentinput dataFunctionnamed statementsReturn valuecomputed resultCalleruses the result
How does a value move from the caller into a function, get used by its statements, and come back 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.

entersreturns6argument12return valuedoublefunction
How do the supplied argument and the function's statements determine the value returned to the caller?

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.

supplies a value fornumberfunction input name6supplied input value
How does a function's parameter correspond to the argument supplied in a specific function call?
TermWhere it appearsRole
ParameterIn the functionNamed place that receives input
ArgumentIn the function callValue or variable passed into the function
Return valueAfter the function performs its workResult 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.

thenprovide data toproduceFunction nameidentifyArgumentssupply inputFunction statementsperform computationReturn valuesend result back
What happens first when code uses a function name, supplies arguments, executes the function, and receives a return value?
startspasses data toproducesFunction calluse the nameReceive inputparameter receives argumentComputerun statementsReturn resultsend value back
What statements belong inside a function, and how does calling its name cause those statements to run?

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?

  • The function name
  • 4
  • 7
  • The parameter name
Reveal answer

Answer: 7

The supplied value 4 is the argument. The function's statements add 3, so the return value is 7.

EASY

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

  1. A function is a named sequence of statements that performs a computation when called.
  2. A function call includes a function name, argument or arguments, and a return value.
  3. A parameter is the function-side name or place for input; an argument is the value or variable supplied by the caller.
  4. Arguments flow into a function, and the function's computation produces a return value for the caller.
  5. 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.