0 votes
in C Plus Plus by

What are Default Parameters? How are they evaluated in the C++ function?

1 Answer

0 votes
by

Default Parameter is a value that is assigned to each parameter while declaring a function.

This value is used if that parameter is left blank while calling to the function. To specify a default value for a particular parameter, we simply assign a value to the parameter in the function declaration.

If the value is not passed for this parameter during the function call, then the compiler uses the default value provided. If a value is specified, then this default value is stepped on and the passed value is used.

Example:

int multiply(int a, int b=2)

{

              int r;

              r = a * b;

              return r;

  

int main()

 {

  

             Cout<<multiply(6);

             Cout<<”\n”;

             Cout<<multiply(2,3);

 }

Output:

12

6

As shown in the above code, there are two calls to multiply function. In the first call, only one parameter is passed with a value. In this case, the second parameter is the default value provided. But in the second call, as both the parameter values are passed, the default value is overridden and the passed value is used.

Related questions

0 votes
asked Nov 29, 2020 in Tableau by SakshiSharma
+1 vote
asked Jan 21, 2021 in C Plus Plus by SakshiSharma
...