_salibo

This week, I learned about RECURSION….


The repetitive process by which a function 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. 


PARTS OF THE RECURSIVE FUNCTION

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. 


EXAMPLE:

factorial (int n)

{
if (n == 1 || n == 0) return 1;
else return (n * factorial (n-1));
}

If the n value is 4, 4 * (factorial (4-1)
if the n value is 3, 3 * (factorial (3-1)
if the n value is 2, 2 * (factorial (2-1)
if the n value is 1, 1



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.

Friday, January 9, 2009

0 Comments:

 
ziee ++ :] BLOG! - Wordpress Themes is proudly powered by WordPress and themed by Mukkamu Templates Novo Blogger