Flylib.com

Books Software

 
 
 

Recursive Functions


Recursive Functions

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 variables for each call, the programmer must guard against permitting too deep a call for the available stack memory. In particular, care must be taken not to have an infinite call.

For example:

image from book


int new_gcd(int numb1, int numb2)


{


return (numb1 < 0)? (new_gcd(-numb1,numb2)):


((numb2 == 0) ? numb1:


new_gcd(numb2 , numb1 % numb2));


}

image from book

This function returns the greatest common divisor of the two numbers : numb1 and numb2 . See NEWGCD.CPP

Another example of recursive functions is the following function:

image from book


unsigned long factorial(int numb)


{


return (numb==0) ? (1) :(numb * factorial(numb-1));


}

image from book

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:

image from book


int value = factorial(5);

image from book

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



Function Name Overloading

In one of the C libraries can be found the following functions:

image from book


iabs()


labs()


fabs()


dabs()

image from book

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):

image from book


if (float_flag)


fabs(x);


else


if (long_flag)


labs(x);


else


if (int_flag)


iabs(x);


else


dabs(x);

image from book

The programmer should not have to work this hard. In C++ one way we can overcome this problem is by using OVERLOADED function names . Function name overloading permits the programmer to use the same function name when the functions have a different signature . This is most useful when the different functions have the same objectives.

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:

image from book

{% 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); }

image from book

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 operate on a new argument. Rather than rewriting all previous code to include this new argument, the programmer only needs to overload the function name with a new definition. The compiler will then determine from context which name to use. What overloading has done is to reduce the complexity of programming by reducing the number of different function names for the programmer to remember.

Q : When should functions be overloaded?

A : When there exists a group of functions with similar results but different signatures. However, the real answer to this question will only come with experience. There will be times in which another technique will be a better choice.

Q : How does a compiler overload a function name?

A : In reality the compiler does not use the same name for each function. It performs a process called NAME MANGLING . Each compiler uses its own specific method to change the name. In general the programmer does not need to be concerned about this process. However, there are cases where care must be taken. For this you need to consult the compiler documentation.

Name mangling can cause problems especially when dealing with C libraries or when moving function names across file boundaries. Should it be necessary to work in Windows with DLL (dynamic linked library) files, name mangling may cause trouble. Name mangling causes trouble because the name in the program used by the compiler no longer agrees with its definition's name.

See OVERLOD2.CPP and OVERLOD3.CPP