The repetitive process by which a functions calls itself is called recursion or circular definition. This is a way of defining something in terms of itself. A function is said to be recursive if a statement in the body of the function calls the function that contains it.
Base Case. This is the part of the recursive function that is found on the if clause. This contains the condition that should be satisfied at one point of execution to terminate the repetitive process done by the recursive function.
General Case. This is the part of the recursive function that is found on the else-clause. This contains the function call of the recursive function to itself.
Give the output of the ff. program when the value entered for a=5.
#include
main()
{
int a, b;
clrscr();
printf(“Enter a value:”);
scanf(“%d”, &a);
b= solve (a);
printf(“The new value is %d”, b);
getch();
solve (int a)
{
if (a == 1) return 2;
else return (solve (a-1) + 2);
}
The output is:
Enter a value: 5
The new value is 10.
Direct and Indirect Recursion
Direct recursions are recursive functions that can call itself through a function call directly inside the body of the function. Indirect recursions are recursive functions that can call another functions outside its function.
No comments:
Post a Comment