1// Basic Recursive function
2// It works in all languages but lets try with C
3
4// lets make a greedy strlen
5
6int my_recursive_strlen(int index, char *str) // note that index must be set at 0 to work in this case
7{
8 if (str[index] == '\0')
9 return index + 1;
10 else
11 return my_recursive_strlen(index++); // i send index + 1 to next function call
12 // this increment index in our recursive stack
13}
14
15void main(void)
16{
17 printf("length of Bonjour = %d\n", my_recursive_strlen(0, "bonjour"));
18}
19
20//output : length of Bonjour = 7
21
22// Recursive functions are greedy and should be used in only special cases who need it
23// or who can handle it.
24
25// a function become recursive if she calls herself in stack
1// Basic Recursive function
2// It works in all languages but lets try with Javascript
3
4// lets make a greedy factorial calculator
5
6function my_recursive_factorial(nb) // nb to compute his factorial
7{
8 if (nb < 0 || nb > 12) // error case
9 return 0;
10 if (nb == 1 || nb == 0)
11 return 1; // you have to check limit for factorial
12
13 return nb * my_recursive_factorial(nb - 1); // ! Recursive call !
14}
15
16//This will find factorial from 1 to 12 with recursive method
17
18// Recursive functions are greedy and should be used in only special cases who need it
19// or who can handle it.
20
21// a function become recursive if she calls herself in stack