A parameter is a special kind of variable used in a function to refer to one of the pieces of data provided as input to the function. These pieces of data are the values of the arguments with which the function is going to be called/invoked. An ordered list of parameters is usually included in the definition of a function, so that, each time the function is called, its arguments for that call are evaluated, and the resulting values can be assigned to the corresponding parameters.
Recall that the modular programming approach separates the functionality of a program into independent modules. To separate the functionality of one function from another, each function is given its own unique input variables, called parameters. The parameter values, called arguments, are passed to the function when the function is called. Consider the following function pseudocode:
Function CalculateCelsius (Real fahrenheit) Declare Real celsius Assign celsius = (fahrenheit - 32) * 5 / 9 Return Real celsius
If the CalculateCelsius function is called passing in the value 100, as in
CalculateCelsius(100), the parameter is
fahrenheit and the argument is
100. The terms parameter and argument are often used interchangeably. However, parameter refers to the variable identifier (fahrenheit) while argument refers to the variable value (100).
Functions may have no parameters or multiple parameters. Consider the following function pseudocode:
Function DisplayResult (Real fahrenheit, Real celsius) Output fahrenheit & "° Fahrenheit is " & celsius & "° Celsius" End
If the DisplayResult function is called passing in the values 98.6 and 37.0, as in
DisplayResults(98.6, 37.0), the argument or value for the fahrenheit parameter is 98.6 and the argument or value for the celsius parameter is 37.0. Note that the arguments are passed positionally. Calling
DisplayResults(37.0, 98.6)would result in incorrect output, as the value of fahrenheit would be 37.0 and the value of celsius would be 98.6.
Some programming languages, such as Python, support named parameters. When calling functions using named parameters, parameter names and values are used, and positions are ignored. When names are not used, arguments are identified by position. For example, any of the following function calls would be valid:
CalculateCelsius(98.6, 37.0) CalculateCelsius(fahrenheit=98.6, celsius=37.0) CalculateCelsius(celsius=37.0, fahrenheit=98.6)
- A value provided as input to a function.
- A variable identifier provided as input to a function.