Skip to main content

Section 5.5 Parameters and Arguments

Subsection 5.5.1 Basics

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.
int myFunction(int x, int y) {
  ...
}
Listing 5.5.1. myFunction has two parameters, x and y.
int main() {
    int result = myFunction(2, 4);
}
Listing 5.5.2. The arguments 2, 4 are being passed to myFunction in a call made from main.

Checkpoint 5.5.1.

Subsection 5.5.2 Parameter Numbers and Types

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.
Valid parameter lists:
Invalid parameter lists:
  • int foo { (missing the parameter list)
  • int foo(int x, y) { (no type for y)
  • int foo(int x double y) { (no comma between x and y)

Checkpoint 5.5.2.

Which of the following is a correct function header (first line of a function definition)?
  • totalcost (double cost, tax, discount)
  • totalcost needs a return type, and each parameter needs a data type.
  • totalCost (double cost, double tax) {
  • totalcost needs a return type.
  • void totalCost (double cost, double tax, double discount) {
  • Correct!

Subsection 5.5.3 Argument Types

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:
  1. 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.
  2. 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) )
  3. 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.

Checkpoint 5.5.3.

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;
double multiply(int num, double other) {
  ...
  • multiply(int x, double p);
  • Data types are not needed when calling a function.
  • multiply(x, p);
  • Correct!
  • multiply(double p, int x);
  • Those are in the wrong order. And, we do not specify data types when passing arguments.
  • multiply(p, x);
  • Those are in the wrong order.

Subsection 5.5.4 Passing is by Value

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.
Listing 5.5.3.

Insight 5.5.1.

Pass by value means that values are passed to functions. In pass by value you never really pass a variable to a function.
You have attempted of activities on this page.