+ 2
How to use recursive functions?
Help me! To use of recursive function in c.
1 ответ
+ 2
A recursive function is one which calls itself.
To stop an infinite amount of calls, you need to define a base case.
Your question is asking how to use them.
In your C program - as with any other function - define your function first (before main() and after any included headers)
Then, you will now be able to use this recursive function. The SoloLearn C tutorial outlines an example but here’s one too:
void recursive(int z);
int main()
{
recursive(8);
//edit, adding a return statement
return 0;
}
void recursive(int z)
{
if(z < 0)
{ return; }
else
{
//do something
recursive(z-1);
}
}
It’s a pointless program but the fact recursive() calls itself means it is a recursive function. Note there is a base case/return statement to stop infinite calls.