Take the 2-minute tour ×

Can anyone tell me the main difference between an inline function and recursive function?

share|improve this question
Homework? Nothing wrong with homework questions, but they should be tagged as such :) –  Binary Worrier May 9 '10 at 10:49
Why do you specify "in C++"? Are you familiar with the concepts in other languages? –  Matt Curtis May 9 '10 at 10:52
It seems that many are using the inline keyword explicitly and not explaining what the compiler does when it inlines functions. Like many are saying, the inline keyword is a hint, but it is generally useless as the compile can and does make the decision for itself whether it is there or not. yosefk.com/c++fqa/inline.html Also, inline may not be faster and may increase your code size which can have a negative effect on caches. That is why it is good that the compile makes this decision. –  Beached May 10 '12 at 13:27

3 Answers 3

These are unrelated concepts.

An function may be declared inline, which signals to the compiler that any calls to the function should be replaced by an implementation of the function directly at the point the call is made. It's vaguely like implementing some piece of logic as a macro, but it retains the clean semantics of a normal function call.

A recursive function is simply one that calls itself.

Note that the inline keyword is just a suggestion. The compiler is free to ignore it whenever it wants.

Also note that a recursive can also be declared inline. The compiler may, in principle, be able to inline a recursive function by transforming it into an iterative algorithm inside the calling function. However, recursion is usually one of the things that will make a compiler give up on inlining.

share|improve this answer
Not true, tail-recursion is a common optimization in modern day compilers, and THE thing that makes functional languages like haskell and f# work. –  Blindy May 9 '10 at 10:59
@Blindy: Which of my statements is not true? –  Marcelo Cantos May 9 '10 at 11:04
Tail-recursion is THE thing that makes functional languages work? Not true at all, it's an optimization in order to decrease the stack space use and to improve efficiency and there's nothing that requires it. Other than it makes it more efficient of course. Furthermore Common Lisp's standard does not define it although most implementations do. –  Jonas May 9 '10 at 11:21
@Jonas: That is not true. The guaranteed presence of TCO changes the semantics of the language in a significant way: Only with this guarantee may the programmer use unbounded recursion. Indeed, for FP languages that specify TCO, TCO becomes a central semantic property that allows the correct operation of a whole category of programming that would have to be written differently in the absence of TCO. See this question and my two comments: stackoverflow.com/questions/1888702/… –  harms May 9 '10 at 12:12
@Péter: I wouldn't add "directly or indirectly". A pair of functions achieves recursion, but it is the pair, as a system, that is recursive, not the individual functions. Perhaps this is a subjective issue, though; I have had a look and failed to locate a definition of "recursive function" that explicitly includes or excludes indirect recursion. –  Marcelo Cantos May 9 '10 at 14:45

These are two very different concepts. 99% of programming languages allow recursive functions. A recursive function re-calls it's self to get something done. Most recursive functions can be rewritten as loops.
E.g. Simple recursive function.

int Factorial(int f)
   if(f > 1)
       return f * Factorial(f-1);
       return 1;

An in-lined a function is a hint to the compiler that you don't want the processor to jump to this function, instead just include the op-codes for the function where ever it is used. This builds faster code for some calls one some architectures. Be aware that most modern compilers targeting non-embedded processors will choose to ignore your "inline" hints, and will choose what to inline itself.

Hope this helps, apologies if badly formatted, typed on my I-phone.

share|improve this answer
Fixed some of your "helpful" iPhone auto-corrections :) –  Thorarin May 9 '10 at 11:14
@thorain: Thanks dude :) –  Binary Worrier May 9 '10 at 14:50

A recursive function is a function that calls itself.

An inlined function is a function, that is "inserted into another function", i.e. if you have an inlined function add(a,b), and you call it from a function func, the compiler may be able to integrate the function body of add into the body of func so the arguments don't need to be pushed onto the stack.

share|improve this answer

Your Answer


