Hi
is it possible to have recursive function as inline function ?
this was question asked interview
please help me

Inline is just a request to the compiler to treat the call to a function like a macro and substitute it rather than treating it as a function and making a call. This request may or may not be considered by a compiler where in the second case it treats it as a normal function rather than inline. So even though you do try to make a recursive function inline the compiler will ignore it and consider it as a normal function and work in the normal fashion to execute it.
P.S Doing this is definitely not a good coding practice.

im assuming you are not in your interview right now and are at you pc...so you could just try it out and see ;)

Basically, if the compiler wanted to inline a recursive function, it would be the equivalent to unrolling a loop. The first rule is that the number of recursions (or loops) has to be determined at compile time. So if your recursive function cannot be turned into a for-loop with a fixed number of iterations, then sometimes the compiler has a setting in its optimization options that determines a small fixed amount of unrolling that it will do before calling the normal function. So it is possible that the compiler chooses to put a few inline copies of the function before it calls it normally (essentially, this will be faster if "usually" only a few recursions are needed, otherwise it makes no difference). Generally speaking, the compiler cannot fully inline a recursive function because it would lead to infinite amount of compiled code. And the special cases are when the recursion dept can easily be determined at compile-time and is very shallow (only a few recursive calls), or when optimization settings prescribe that a few recursions are unrolled (i.e. inlined) before the non-inline function call is performed.

This article has been dead for over six months. Start a new discussion instead.