Image

C++ - Functions - 5. Default argument function

5. Default argument function

In C++ programming you can provide default values for function parameters. The idea behind default arguments is very simple. If a function is called by arguments those arguments used by function.

e.g:
#include<iostream.h>
#include<conio.h>
void display(char='*',int=1);
void main()
{
  clrscr();
  cout<<"\nNo argument is passed    ";
  display();
  cout<<"\nFirst argument is passed ";
  display('#');
  cout<<"\nBoth argument is passed  ";
  display('$',5);
  getch();
}
void display(char c,int n)
{
  for(int i=0;i<n;++i)
  cout<<c<<" ";
}
O/P
No  argument is passed
Char=*
First argument is passed
Char=#
Both rgument is passed
Char=$
Char=$
Char=$
Char=$
Char=$