Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site [in Chinese or English] for best site performance. Other MathWorks country sites are not optimized for visits from your location.
void Twice[int& a, int& b] { a *= 2; b *= 2; }
- Note that when it is run, the variables passed into Twice from the main[] function DO get changed by the function
- The parameters a and b are still local to the function, but they are reference variables [i.e. nicknames to the original variables passed in [x and y]]
void Func2[int& x, double& y] { x = 12; // these WILL affect the original arguments y = 20.5; }
int num; double avg; Func2[num, avg]; // legal Func2[4, 10.6]; // NOT legal Func2[num + 6, avg - 10.6]; // NOT legal
int Task1[int x, double y]; // uses return by value int& Task2[int x, double y]; // uses return by referenceThis is a trickier situation than reference parameters [which we will not see in detail right now].
A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions.
For example:
function example[parameter] {
console.log[parameter]; // Output = foo
}
const argument = "foo";
example[argument];
Note the difference between parameters and arguments:
- Function parameters are the names listed in the function's definition.
- Function arguments are the real values passed to the function.
- Parameters are initialized to the values of the arguments supplied.
Two kinds of parameters:
the most common kind; they pass values into functions. Depending on the programming language, input parameters can be passed in several ways [e.g., call-by-value, call-by-address, call-by-reference].
The sample function writeout.cpp asks the user for a number and where they want it written. Depending on their answer, main will pass to the writout function either cout, cerr, or an open file stream, and the function will write to the appropriate location without knowing the difference.
Dave Braunschweig
Overview
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.
Discussion
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: