Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a cyclic function and a recursive function?

I would like a brief description of the difference between cyclic functions and recursive functions with example.

For example, let us assume we have 3 functions A B and C. I know that if function A calls same function its called recursion. What about if A calls B, then B calls C, and then C calls A?

Can anyone clarify this?

like image 252
Satya Mahesh Avatar asked Feb 05 '26 20:02

Satya Mahesh


2 Answers

It is called indirect recursion.

void foo(void)
{
    bar();
}

void bar(void)
{
    foo();
}

In the C Standard (emphasis mine):

(C99, 6.5.2.2p11) "Recursive function calls shall be permitted, both directly and indirectly through any chain of other functions."

Without an exit condition (like in my example), you likely end up in a stack overflow (stack frames are infinitely added at each call until stack is full).

like image 183
ouah Avatar answered Feb 09 '26 09:02

ouah


Recursive function is a function that calls itself.

It may call itself directly (A calls A), or indirectly (A calls B, B calls C, C calls A). It's still a recursion.

like image 35
nullptr Avatar answered Feb 09 '26 07:02

nullptr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!