how to make recursive function in C
// Basic Recursive function
// It works in all languages but lets try with C
// lets make a greedy strlen
int my_recursive_strlen(int index, char *str) // note that index must be set at 0 to work in this case
{
if (str[index] == '\0')
return index + 1;
else
return my_recursive_strlen(index++); // i send index + 1 to next function call
// this increment index in our recursive stack
}
void main(void)
{
printf("length of Bonjour = %d\n", my_recursive_strlen(0, "bonjour"));
}
//output : length of Bonjour = 7
// Recursive functions are greedy and should be used in only special cases who need it
// or who can handle it.