C permits nesting of function in c freely. main can call function1, which calls function2, which calls function3, ….. and so on. There is principle no limit as to how deeply functions can be nested.
Consider the following program
float ratio (int x, int y, int z); int difference (int x, int y); main() { int a, b, c; scanf("%d %d %d", &a, &b, &c); printf("%f \n", ratio(a,b,c)); } float ratio(int x, int y, int z) { if(difference(y, z)) return(x/(y-z)); else return(0.0); } int difference(int p, int q) { if(p !=q) return (1); else return(0); }
The above program calculates the ration
ab-c
and prints the result. we have the following three functions main()
ratio()
difference()
main reads the values of a, b and c and calls the function ratio to calculate the value a/(b-c). The ratio cannot be evaluated if (b-c) = 0. Therefore, ratio calls another function difference to test whether the difference (b-c) is zero or not; difference returns 1, if b is not equal to c; otherwise return zero to the function ratio. In turn, ratio calculates the value a/(b-c) if it receives 1 and returns the result in float. In case, ratio receives zero from difference, it sends back 0.0 to main indicating that (b-c) = 0.
Nesting of function calls in also possible. For example, a statement like P = mul(mul(5,2),6); is valid. This represents two sequential function calls. The inner function call is evaluated first and the returned value is again used as an actual argument in the outer call. If mul returns the product of its arguments, then the value of p would be 60 (=5 x 2 x 6).
Note that the nesting does not mean defining one function within another. Doing this illegal.
Recursion
When a called function in turn calls another function a process of ‘chaining’ occurs. Recursion is a special case of this process, where a function calls itself. A very simple example of recursion is presented below.
main()
{
printf(“This is an example of recursion\n”)
main();
}
When executed, this program will produce an output something like this This is an example of recursion, This is an ex – Execution is terminated abruptly; otherwise the execution will continue indefinitely. Another useful example of recursion is the evaluation of factorials of a number. The factorial of a number n is expressed as a series of repetitive multiplication as shown below.
factorial of n =n(n-1)(n-2)….1.
For example,
factorial of 4 = 4x3x2x1 = 24
A function to evaluate factorial of n is follows
factotial(int n) { int fact; if (n==1) return(1); else fact = n*factorial(n-1); return(fact); }
Let us see how the recursion works. Assume n =3. Since the value of n is not 1, the statement fact = n * factorial(n-1); will be executed with n=3. fact = 3 * factorial(2); will be evaluated. The expression on the right hand side includes a call to factorial with n = 2. This call will return the following value 2 * factorial(1).
Once again, factorial is called with n=1. This time, the function returns 1. The sequence of operations can be summarized as follows, fact = 3 * factorial(2) = 3 * 2 * factorial(1) = 3 * 2 * 1 = 6
Recursive functions can be effectively used to solve problems where solution is expressed in terms of successively applying the same solution to subsets of the problem. When we write recursive functions, we must have an if statement somewhere to force the function to return without the recursive call being executed. Otherwise, the function will never return.
Read More Topics |
Static function in C++ |
Passing pointers to functions |
Const member function |
Inline function in C++ |