Tuesday 16 December 2014

Function overloading Concept with lot of examples

The following rules are applicable for implementing function overloading concept.

1)      Function name should not be a keyword. Meaningful name should be given to function names and variables.
1)      C++ allows more than one function with the same name with signature. This concept is called “function overloading”.
Signature means either the number of parameters or data types of parameters or order of parameters should be different. Return type should not be considered as a signature.

C++ compiler is strict checking type. If we pass integer variables as arguments then calling function goes to 1st block in above program.
This concept is called function overloading. In-spite of we are passing so many arguments instead of required arguments, only 1st few arguments will be taken into consideration. 
 Here 3rd parameter is rate of interest. It always takes 5 as rate of interest. It won’t show errors inspite of we are passing two arguments.
Compile time error.

        While implementing default arguments, we should not overload the function with leftover arguments.

Here the default argument is r=5. If we pass two arguments then the compiler gets confused to select particular function.

Here this is valid. Instead of using function overloading concept, by making additional arguments as zero, we can get better results for same function.