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 ?
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
inlineyour function?
With recursive functions compilers will usually look for the opportunity to perform tail call optimization. Your function is not tail call recursive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With