Another important area where functions are used is recursion. Recursion
is a repetitive process here a function calls itself.That is in a
recursive process a function is calls it self. Recursion is a very
important application in every where, including real life also.
For Example if you observed in a screen recording, where a smaller
window is appear as multiple windows. Like below.
Recursion in a screen recording program, where the smaller window contains a snapshot of the entire screen |
The same thing is also useful while solving a complex problem in
computer science. Using the recursion the larger problem can be defined
in two cases first base case and second general case. In base case the
solution to the complex problem is simple and it can be solved easily.
But in the general case the complex problem is divided into Small units
until we reach the base case condition. Here is best example for
recursion how to find factorial of a number using recursion.
The factorial of a given number is computed as using recursion is
factorial(0) = 1;
factorial(n) =n*(n-1)*(n-2)*(n-3)...............*1
In the piece of code the first line is the base case and the
second line is the general case. In the first line the solution is comes
directly, where as in the second line, the problem is reduced until the
base case solution is appeared. This type of solving a problem is known
as recursion. They are so may types of recursion.
Tail recursive functions are highly desirable because they are much more efficient to use as the amount of information that has to be stored on the system stack is independent of the number of recursive calls.
Tail Recursion
A recursive function is said to be tail recursive if no operations are pending to be performed to be performed when the recursive function returns to its caller. When the called function returns, the returned value is immediately returned from the calling function.Tail recursive functions are highly desirable because they are much more efficient to use as the amount of information that has to be stored on the system stack is independent of the number of recursive calls.
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.