Recursive Function And Define Directive In C Language
Recursive Function:
A recursive function is a function that calls itself either directly or indirectly through another function.
Recursive function calling is often the simplest method to encode specific types of situations where the operation to be encoded can be eventually simplified into a series of more basic operations of the same type as the original complex operation.
This is especially true of certain types of mathematical functions. For example to evaluate the factorial of a number, n
n! = n * n-1 * n-2 * ... * 3 * 2 * 1.
We can simplify this operation into
n! = n * (n-1)!
where the original problem has been reduced in complexity slightly. We continue this process until we get the problem down to a task that may be solved directly, in this case as far as evaluating the factorial of 1 which is simply 1.
So a recursive function to evaluate the factorial of a number will simply keep calling itself until the argument is 1. All of the previous (n-1) recursive calls will still be active waiting until the simplest problem is solved before the more complex intermediate steps can be built back up giving the final solution.
For Example : Program to evaluate the factorial of a number using recursion.
CODE/PROGRAM/EXAMPLE
#include <stdio.h>
short factorial( short ) ;
void main()
{
short i ;
printf(“Enter an integer and i will try to calculate its factorial : “ ) ;
scanf( “%d”, &i ) ;
printf( “
The factorial of %d, %d! is %d
”, i, i, factorial( i ) ) ;
}
short factorial( short num )
{
if ( num <= 1 )
return 1 ;
else
return ( num * factorial( num - 1 ) ) ;
}
This program will not work very well as is because the values of factorials grow very large very quickly. For example the value of 8! is 40320 which is too large to be held in a short so integer overflow will occur when the value entered is greater than 7. Can you offer a solution to this ?
While admittedly simple to encode in certain situations programs with recursive functions can be slower than those without because there is a time delay in actually calling the function and passing parameters to it.
There is also a memory penalty involved. If a large number of recursive calls are needed this means that there are that many functions active at that time which may exhaust the machine’s memory resources. Each one of these function calls has to maintain its own set of parameters on the program stack.
#define directive:
This is a preprocessor command which is used to replace any text within a C program with a more informative pseudonym.
For Example :- #define PI 3.14159
When the preprocessor is called it replaces each instance of the phrase PI with the correct replacement string which is then compiled. The advantage of using this is that if we wish to change the value of PI at any stage we need only change it in this one place rather than at each point the value is used.
Macros:
Macros make use of the #define directive to replace a chunk of C code to perform the same task as a function but will execute much faster since the overhead of a function call will not be involved. However the actual code involved is replaced at each call to the macro so the program will be larger than with a function.
For Example :- Macro to print an error message.
Syntax
#define ERROR printf( "\n Error \n" )
void main( )
{
...
if ( i > 1000 )
ERROR ; /* note must add ; in this case to make correct …C statement */
}
Macros can also be defined so that they may take arguments.
For Example :-
CODE/PROGRAM/EXAMPLE
#define PR( fl ) printf( "%8.2f ", fl )
void main()
{
float num = 10.234 ;
PR( num ) ;
}
What the compiler actually sees is : printf( “%8.2f ”, num ) ;
While admittedly advantageous and neat in certain situations care must be taken when coding macros as they are notorious for introducing unwanted side effects into programs.
For Example :-
CODE/PROGRAM/EXAMPLE
#define MAX(A, B) ( ( A ) > ( B ) ? ( A ) : ( B ) )
void main( )
{
int i = 20, j = 40 , k ;
k = MAX( i++, j++ ) ;
printf( " i = %d, j = %d\n", i, j );
...
}
The above program might be expected to output the following
i = 21, j = 41
whereas in fact it produces the following
i = 21, j = 42
where the larger value is incremented twice. This is because the macro MAX is actually translated into the following by the compiler
( ( i++ ) > ( j++ ) ? ( i++ ) : ( j++ ) )
so that the larger parameter is incremented twice in error since parameter passing to macros is essentially text replacement.