Tuesday, November 18, 2014

3.2.5 RECURSIVE FUNCTIONS


§  In C, it is possible for the functions to call themselves.

§  A function is called ‘recursive’ if a statement within the body of a function calls the same function.

 

NOTE: There must be an exclusive stopping statement otherwise the function will not be terminated.

 

General Syntax:

return_type recursive_function_name(parameter list)

{

                        local variable declaration;

…….

terminating condition;

……

value=recursive_function_name(expression);

return(value);

}

 

Example:


 

Difference between recursion and iteration.

 

Recursion
Iteration
Repetitive execution of a function until stopping condition is met, is called recursion.
Repetitive execution of one or more statements, is called iteration, commonly known as loop.
Recursion is like a top-down approach to problem solving as it divides the problem into pieces or selects one key step, postponing the rest.
Iteration is more of a bottom-up approach as it begins with what is known and from this constructs the solution step by step.

 
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.