Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline for recursive function

The inline function is replaced by its code in the phase when the gcc generate the asm code.

What will be the behaviour when using inline for recursive function

inline int fact (int n)
{
    if (n<=1)
        return 1;
    else
        return (n * fact(n-1));
}

I generate the asm code with gcc -S when the recursive function is with inline prefix and when the recursive function is without the inline prefix and I found that the asm code for both cases is the same.

Have You any explanation for that ?

like image 925
MOHAMED Avatar asked Feb 23 '26 01:02

MOHAMED


1 Answers

Note that inline is just an suggestion to the compiler which compiler may or may not accept. It is not binding on the compiler to adhere to your suggestion. An intelligent compiler will inline a function if it can even without the suggestion. Usually, for recursive functions compilers will do till certain depths.

Why the compiler does not inline your function?

With recursive functions compilers will usually look for the opportunity to perform tail call optimization. Your function is not tail call recursive.

like image 150
Alok Save Avatar answered Feb 25 '26 16:02

Alok Save



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!