• As we know that in C programming language it is compulsory to pass all arguments during function call, which are used in function definition, but C++ allows us to call a function without specifying all its arguments by using default argument.
  • In default argument, computer assigns the default value to the argument for which there no argument is specified during the function call. Default values are specified during function declaration but there is no change in function definition.
  • Default values are assigned only when no arguments are passed during the function call otherwise the value which is passed during the function call will be assigned. The following example demonstrates function declaration with default argument.
double power(float x, int y = 2);       // function declaration
  • We can pass the default arguments from right to left position. Following are some examples of declaring default arguments:
float sum(float x, float y, float z = 10.0);                 // legal
float sum(float x = 10.0, float y, float z);                 // illegal
float sum(float x = 10.0, float y = 10.0, float z);         // illegal
float sum(float x , float y = 10.0, float z);               // illegal
float sum(float x, float y = 10.0, float z = 10.0);         // legal
float sum(float x = 10.0, float y = 10.0, float z = 10.0);   // legal
  • Default argument is useful when certain argument uses some fixed value many time. In above example we are using generally y=2 to determine the square of number so we have used default argument 2. All the default argument must be placed at the last in function declaration. The complete program with default argument is given below:
double power(float x, int y = 2);       // function declaration

main( )
{
    // function call using default argument (will assign y = 2)
   cout << power(5) ;
    // function call without using default argument (will assign y = 3)
   cout << power(5,3);
}

double power(float x,int y) 
{
   int i;
   double ans = 1;
   
   for(i = 1 ; i <= y ; i++ ) 
   {
         ans = ans * x;
   }

   return(ans);
}
  • In above code snipped default value 2 is assigned to the argument ‘y’ when we are not passing ‘y’ value during function call otherwise the value of ‘y’ is equal to value passed during the function call.

Constant Argument:

  • Constant arguments are those arguments, which cannot be modified inside the function definition code.
  • We can declare constant argument by using keyword const before the argument declaration in function definition.
  • When we try to change the value of the constant argument compiler will generate error.
void function-name ( const data-type);   // function declaration with const argument
  • The following program demonstrates the use of constant argument in display function.
void display ( const int );   // function declaration with const argument

main()
{
    display(50);   // calling function with constant argument
}

void display (const int x)
{
    cout << "X is : " <<x;
    x = 100 ;                // results in error. cannot modify
    cout << "X is : " <<x; 
}
  • While working with constant argument, it is compulsory to use keyword const in both function declaration and definition. In C program we can modify the value of constant variable and argument by using scanf statement while it is not possible in C++.