Recursive functions
can be used in C and also in C++. A function is recursive, if it calls itself directly or indirectly through another function. Each time the recursive function is called, it begins over again. A new set of arguments and local variables are created on the stack. There will be one set of variables for each call. Because there is a set of
For example:
|
|
int new_gcd(int numb1, int numb2)
{
return (numb1 < 0)? (new_gcd(-numb1,numb2)):
((numb2 == 0) ? numb1:
new_gcd(numb2 , numb1 % numb2));
}
|
|
This function returns the
greatest common divisor
of the two
Another example of recursive functions is the following function:
|
|
unsigned long factorial(int numb)
{
return (numb==0) ? (1) :(numb * factorial(numb-1));
}
|
|
This function will return the factorial of the number: numb . In mathematics we would represent this by numb! . A call to this function could be:
|
|
int value = factorial(5);
|
|
in which case value would be 120 that is 5! . In order to execute this call there would need to be 6 copies of numb created on the stack, one for each number up to 5 . However there will be only one copy of the function in memory. See NEWFACT.CPP
In one of the C libraries can be found the following functions:
|
|
iabs() labs() fabs() dabs()
|
|
These functions return the absolute value of an int , a long , a float , and a double respectively. You may asked yourself: "Why do we need four functions to accomplish what appears to be the same objective?" To use these functions requires a lot of code. For example (where the "flags" contain values to permit the calling of the correctly function):
|
|
if (float_flag) fabs(x); else if (long_flag) labs(x); else if (int_flag) iabs(x); else dabs(x);
|
|
The programmer should
not
have to work this hard. In C++ one way we can
Overloading was done when the same operator symbols: + , -, / and * were used on integer s, long s, float s or double s. These are different data types and therefore the function arguments would have to be different, yet the objectives are the same and the operator names are the same. Notice further that overloading was used earlier when the objects cout and cin with the operators << and >> respectively operated regardless of what data type of argument was used. In addition << and >> were overloaded when they used previously with file input and output.
In C++, two or more functions within the same scope may have the same name provided they have different signatures . Two functions have the same signatures, if their signatures have all of the following:
the same number of arguments
the data types are the same
the data types and arguments are in the same order
So to have overloaded functions one of these must be false. The output data type in no way effects the overloading of function names.
For example:
|
|
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
int MAX(int a, int b)
{ return (a>b?a:b); }
long MAX(long a, long b)
{ return (a>b?a:b); }
float MAX(float a, float b)
{ return (a>b?a:b); }
double MAX(double a, double b)
{ return (a>b?a:b); }
|
|
are functions that overload the name MAX . See OVERLOAD.CPP The C++ compiler will not return an error when the definitions for the functions MAX are compiled since the signatures of these functions are all different.
Note: Earlier C++ compilers required the word "overloaded" to precede the definition of overloaded functions. However, this is no longer necessary in Standard C++.
Overloading may help the programmer who wants a function to
Q : When should functions be overloaded?
A
: When there exists a
Q : How does a compiler overload a function name?
A
: In reality the compiler does
not
use the same name for each function. It
Name mangling can cause problems
See OVERLOD2.CPP and OVERLOD3.CPP