What is the principle reason for using default arguments in the function? Explain how missing arguments and default arguments are handled by the function simultaneously?

This answer is restricted. Please login to view the answer of this question.

Login Now

A function can be called without specifying all its arguments. But it does not work on any general function. The function declaration must provide default values for those arguments that are not specified. When the arguments are missing from function call , default value will be used for calculation.

Example:

#include <iostream>

using namespace std;

int sum(int a, int b = 10, int c = 20);

int main()
{

    cout << sum(11) << endl;

    cout << sum(11, 12) << endl;

    cout << sum(11, 12, 13) << endl;

    return 0;
}

int sum(int a, int b, int c)
{

    int z;

    z = a + b + c;

    return z;
}

The output of above program is

41
43
36
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .