Explain the Rules for using Default Arguments with an example in C++.
A default argument is used by functions to free the programmer from specifying values which can be set to default by the function. E.g. consider a function for printing an integer. A user can be given an option to decide of what base the integer should be printed however in most cases it will be decimal. E.g. void print (int value, int base = 10); //here the base is set to 10 by defaultThe above function can be called as: print (10); //here default base will be taken as 10 print (10, 8); a function can specify a default argument for one or more of its parameters at the initialization time within the parameter list. A function that has a default parameter can be invoked with or without that parameter. If it gets invoked without the parameter then the default value applies to that parameter. Arguments to the call are resolved by position and hence the default arguments can be specified as trailing arguments only.e.g. void display (int a, int b = 0, int c = 9); // correct void display (int a = 0, int b, int c = 9); //wrong void display (int a = 0, int b = 9, int c); //wrong The default arguments are specified in the function declaration and not in the function definition.
