Javatpoint Logo
Javatpoint Logo

Default arguments in C++

In a function, arguments are defined as the values passed when a function is called. Values passed are the source, and the receiving function is the destination.

Now let us understand the concept of default arguments in detail.

Default arguments in C++

Definition

A default argument is a value in the function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument.

Characteristics for defining the default arguments

Following are the rules of declaring default arguments -

  • The values passed in the default arguments are not constant. These values can be overwritten if the value is passed to the function. If not, the previously declared value retains.
  • During the calling of function, the values are copied from left to right.
  • All the values that will be given default value will be on the right.

Example

  • void function(int x, int y, int z = 0)
    Explanation - The above function is valid. Here z is the value that is predefined as a part of the default argument.
  • Void function(int x, int z = 0, int y)
    Explanation - The above function is invalid. Here z is the value defined in between, and it is not accepted.

Code

Output

25
50
80    

Explanation

In the above program, we have called the sum function three times.

  • Sum(10,15)
    When this function is called, it reaches the definition of the sum. There it initializes x to 10 y to 15, and the rest values are zero by default as no value is passed. And all the values after sum give 25 as output.
  • Sum(10, 15, 25)
    When this function is called, x remains 10, y remains 15, the third parameter z that is passed is initialized to 25 instead of zero. And the last value remains 0. The sum of x, y, z, w, is 50 which is returned as output.
  • Sum(10, 15, 25, 30)
    In this function call, there are four parameter values passed into the function with x as 10, y as 15, z is 25, and w as 30. All the values are then summed up to give 80 as the output.

Note If the function is overloaded with different data types that also contain the default arguments, it may result in an ambiguous match, which results in an error.

Example

Output

prog.cpp: In function 'int main()':
prog.cpp:15:20: error: call of overloaded 'sum(int, int)' is ambiguous
  cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
                    ^
prog.cpp:4:5: note: candidate: int sum(int, int, int, int)
 int sum(int x, int y, int z=0, int w=0) // Here there are two values in the default arguments 
     ^
prog.cpp:9:5: note: candidate: int sum(int, int, float, float)
 int sum(int x, int y, float z=0, float w=0) // Here sum is overloaded with two float parameter values    

Explanation

Here when we call the sum function with all the parameters(x, y, z, w) or either any one parameter value of z or w, the compiler gets confused about which function to execute. Thus, it creates an ambiguity which results in the error.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA