Hi all .I need some one to explain what and how recursive functions work...
Thanks lot
Hi all .I need some one to explain what and how recursive functions work...
Thanks lot
Basically, a recursive function is a function which would call itself until a certain condition is TRUE.
Eg.
Notice that if number is not less than 2, then number is multiplied with the return value of another call to factorial().Code:unsigned long factorial(int number) { return (number < 2) ? 1 : number * factorial(number - 1); }
And the obvious.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
What's UP!
RECURSION--is the ability of a function to cal itslef, either directly or indirectly.
EX>>>.
#include<stdio.h>
void count_down(int n); /*function prototype*/
int main(void)
{
count_down(10); /*recursive function call itself over and over again until it reaces 0*/
return 0; /*program completed sucessfully*/
}
void count_down(int n) /*function definition*/
{
if (n) {
printf("%d ! ", n);
count_down(n-1); /*recursive function that counts down to 0 then prints BLAST OFF!*/
}
else
printf("\nBLAST OFF\n");
}
Hope this example helps so complie, run it to see, huh!?!?
"Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
-Bruce Lee