GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Programming  >  C++

 Print  |  
Question:  Default Parameter

Answer: What is default parameter? Can a overloaded function have a default parameter?


April 04, 2009 15:29:38 #2
 dipuprits   Member Since: April 2009    Total Comments: 3 

RE: Default Parameter
 
Default parameter is a default value for a parameter to a function.
 
The default value can be provided to the list of parameters starting from the right end. Ex: for a function fun(int i, float f, long l), you can specify default value as fun( int i, float f, long l = 1000) but not fun( int i = 10, float f, long l).

You can specify default value for overloaded function provided it is still different from the other forms when the default value is applied.

Ex: void Fun( int i );
void Fun( int i, int j, float f);
void Fun( int i, int j = 10);   // Ambiguous call.

the third version is not allowed as the compiler cannot decided which of the two to use when there is a call to this function like Fun(10) since it could either be Fun(10) or Fun(10,10).
     

 

Back To Question