We’ve mentioned that you pass values to a function. This is mostly true.
When you pass a hard coded value as your argument, you always pass the value. See the example below.
int largerNumber = maxValue(8, 12);
If you pass a primitive data type (int, float, long, double, char, bool – just to name a few examples), you pass by value. That means that if a value is changed inside the function, when you return, the functional value is the same.
void updateValue(int value)
{
value++;
}
int main()
{
int argument = 10;
cout << "Before function call: " << argument << endl;
updateValue(argument);
cout << "After function call: " << argument << endl;
return 0;
}
Arrays, which we’ll look at later, do not get this same treatment, and you can change their original values. That is because it could take too long, and too much resources to duplicate the values inside the function.
The importance of Data Types
Because C/C++ looks at the order that the parameters are passed in, it is important to pass the right type in the right order, otherwise you can get a run time error.
Because some data can be interchanged because it is stored numerically, it is important to make sure you set your arguments and parameters in the correct order.
Passing Variables by Value was originally found on Access 2 Learn
One Comment
Comments are closed.