Thursday, December 18, 2008

3rd-Learnings of the Week 5 [Caligdong]

The eleventh chapter is all about recursion. 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 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 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.

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.


example for recursion:
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);
}

OUTPUT

Enter a value: 5
The new value is 10.

No comments: