C++ default arguments

C++ Default Arguments

C++ language allows us call a function without specifying all its arguments.

In such cases, the function assigns a default value to the parameters which does not have a matching argument in the function call. default values are specified when function is declared. The compiler look at the prototype to see how many arguments(parameters) in a function use and alerts the program for possible default values. in the below is an example of prototype( function declaration) with default values.
for example:

floatamount(float principal, int period, float rate=0.15);

The default value is specified in manner syntactically similar to a variable initialization. in the above example prototype declare a default value of 0.15 to the argument rate. A subsequent function call like

value=amount(5000,7); //so here one arguments missing

passes the value of 5000 to principal variable and 7 to period variable and then lets the function use default value of 0.15 for rate variable. The call

value=amount(5000,5,0.12); // no missing argument

passes an explicit value of 0.12 to rate

A default argument or parameters is checked for type at the time of declaration and evaluated at the time of call. one important point here to note is only the trailing arguments can have defualt at the time of calling. right to left. we can’t provide a default value to a particular argument in the middle of an argument list Some example below of function declaration with default values are

int mul(int i, int j=5, int k=10); // it is legal
int mul(int i=5,int j); // this is illegal
int mul(int i=0, int j, int k=10); // this is illegal
int mul(int i=2,int j=6; int k=90); // it is legal

default arguments are useful in conditions where some arguments(parameters) always have the same value. for instance, bank interest may remaining the same for all customers for a particular period of deposit. it also provides a greater flexibility to the programmers. A function can be written(create) with more parameters than are required for its most common application. Using default arguments, a programmer can use only those arguments that are meaningful to a particular situation.

Example take the example of default arguments
#include <iostream>
using namespace std;
int main()
{
float amount;
float value(float p, int n, float r=0.15); // prototype
void printline(char ch'*', int len=40); // prototype
printline(); // uses default values for arguments
amount=value(5000.00,5) // default for 3rd arguments
std::cout <<"\n fincal value="<<amount"<<"\n \n";
printline('='); // use default value for 2nd argument
return 0;
}
float value(float p, int n, float r)
{
int year=1;
float sum=p;
while(year<=n)
{
sum=sum*(1+r);
year=year+1;
}
return(sum);
}
void printline(char ch,int len)
{
for(int i=1; i<=len; i++)
{
std::cout<<ch;
std::cout<<"\n";
}

Advantage of providing the default arguments are

1. we can use default arguments to add new parameters to the existing functions.
2. Default arguments can be used to combine similar function into one

const ARGUMENTS

in c++, an arguments to a function can be declared as const as shown below

int strlen(const char *p);
int length(const string &s);

The qualifier const tell the compiler that the function(method) should not modify the argument. The compiler will create an error when this condition is violated. This type of declaration is significant only when we passes arguments by reference or pointers

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise