Recall that the name for the extra information a function takes when it is called are the parameters. The values we pass in when the function is called are known as the arguments. We say that the arguments are passed to the function.
Match the function declaration to an example of its function call. (We havenβt learned much about strings, but they are the type of data for pieces of text like "hello".)
A function can have no parameters, one parameter, or many parameters. Each parameter must specify a type and multiple parameters must be separated by commas.
Arguments are matched in order to the parameters of a function. Given int myFunction(int x, int y), the call myFunction(3, 6) is passing 3 to use for the value of x and 6 to use for y. Some programming languages allow you to specify what argument goes with which parameter, but C++ does not. Thus you need to make sure you pass parameters in the correct order. If there is a function double round(double number, int places) and you want to call it to round 3.14159 to 2 decimal places, you must call it with round(3.14159, 2). If you write round(2, 3.14159), you are saying βround 2 to 3.14159 decimal placesβ.
Generally, an argument passed to a function must match the type of that parameter. If you try to pass a value of the wrong type, C++ will look at what is being passed to decide what to do:
If the argument can safely be converted to the parameter type, C++ will do so. For example, if you have a function int myFunction(double x) and you call it with myFunction(3), C++ will convert the integer 3 to the double 3.0.
If the argument can be converted to the parameter type, but the conversion might lose information C++ will give a warning. For example, if you have a function int myFunction(int x) and you call it with myFunction(3.14159), C++ will warn you that information may be lost. As always, you should consider warnings like this to be a giant flashing danger sign. In that function call, x would have the value 3 and you would lose the decimal information.
If you want to lose information, do a cast to explicitly tell the compiler (and other programmers) that you are intentionally changing the type. Something like: myFunction( static_cast<int>(3.14159) )
If the argument cannot be converted to the parameter type, C++ will give an error. For example, if you have a function int myFunction(double x) and you call it with myFunction("hello"), C++ will give an error because it cannot convert the string "hello" into a double.
Which of the following is a legal function call of the function below? Assume you are calling from main, where there are local variables: double p = 3.14; and int x = 12;
As we have seen before, when you pass a variable as an argument to a function, the function receives a copy of the variable. This is called pass by value. The function can change the value of the parameter, but it does not change the original variable.
Consider this program displayed in Codelens. Notice that when y is passed to the function, it is only the value 5 that is passed. That value is copied into x. Anything that happens to x in the function has no impact on y in main.