| | | | | |

Default Arguments

  • Functions may supply default values for some or all of their parameters
  • Start with the last parameter and work backwards
  • The default argument may be placed in either the prototype or the header (part of the implementation), but not both
  • Default argument values used in standard libraries as well as user-defined functions
  • Arguments are substituted until exhausted, then default values are used
  • int next ( int x , int y  = 1 , int z = 0 )
    {
      return (x + y - z);
    }
    
    int main()
    {
      int m, n;
      m = 10;
      n = next(m);     // n = 11 = 10 + 1 - 0
      n = next(m,2);   // n = 12 = 10 + 2 - 0 
      n = next(m,2,3); // n =  9 = 10 + 2 - 3
    }
    

| | Top of Page | 3. Functions in C/C++ - 10 of 13