Advertisement

Inline Functions

Started by December 07, 2001 10:50 PM
5 comments, last by Novice 23 years ago
I have recently been reading about inline functions, that call themselves over and over again until a certain condition is met. I dont really understand the concept that well, and finally, is it important to gaming or is it something I should just ignore and move on?
I think you''re getting recursive functions confused with inline functions.
Advertisement
What you are probably referring to is recursive functions. The do have uses in special situations. Searching a linked list sometimes uses them.
The best example I can think of is calculating the fibonacci number sequence. It is recursive in nature so it is far simpler to write it using a recursive function.
    unsigned long int CalculateValue(n){  //Test for terminal case              				   if(n == 0)               return (0);            else if(n == 1)               return(1);           else               return(CalculateValue(n - 1) + CalculateValue(n - 2));    


This is a little more complex because it has two base cases, but presents the idea. How it works is that when you call it tests to see if it is the terminating(or base) case. If it is it will start rewinding and returning all of the values. If it doesn't, it will call itself again and create another instance of the function on the stack. It will continue to until it meets the terminating case.

Hope that was what you were referring to and that it made sense.

"cogito, ergo sum" -Descartes
[EDIT] formatting

Edited by - conundrum07 on December 7, 2001 12:21:54 AM
"cogito, ergo sum" -Descartes
Mmmm ... recursive inlining ...
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Inline function : The compiler copies the code of the function at every call site instead of doing a function call.

Recursive function : A function that calls itself.

#ifdef RANT
There is no way you can become an effective programmer without understanding the key concepts of computer science. Recursion is one of them (hell, that''s why computers have stacks). I do not believe one can just learn "to program games". You are learning to program. Or not. Period.
#endif

A recursive function - the factorial (how original).
int fact( int i ){  if( i<=1 ) return 1;       // termination  return i * fact( i - 1 ); // recursion}Let''s call f(5).- termination : (5<=1) ? No, move on.- recursion : call f(4).-- termination : (4<=1) ? No, move on.-- recursion : call f(3).--- termination : (3<=1) ? No, move on.--- recursion : call f(2).---- termination : (2<=1) ? No, move on.---- recursion : call f(1).----- termination : (1<=1) ? Yes, return 1, that is f(1) = 1---- return 2*f(1), that is f(2) = 2*1 = 2--- return 3*f(2), that is f(3) = 3*2 = 6-- return 4*f(3), that is f(4) = 4*6 = 24- return 5*f(4), that is f(5) = 120f(5) = 120 


There are two things you have to pay attention to : first, that your termination condition eventually becomes true, and second, that you do not overflow your stack with too many iterations; which means thinking about whether it is worthwhile to use a recursion or not.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Inline function is where the compiler acts like it copies the statements (blocks) that are generally after a function definition, into where ever it''s called from another function.

Recursive functions is what the original poster is thinking about. The thing to watch out for with recursive functions is that it can eat up memory fast. Got to watch out for running out of memory with them. The reason is because each time it recurses it makes a new copy of all variables within the scope of the said function. The exception may be when you passing by reference instead of value. That involves pointers. For more info about this, look into what scope means. This will help you understand why it eats up memory.

and example of inline:

inline int somefunction(int arg1, int arg2); // function prototype

int main()
{
int x, y, result;

result = somefunction(x, y)

return 0;
}

int somefunction(int x, int y)
{
return x * y;
}

Anyway, the line "result = somefunction(x, y)" would be as if in main it''s "result = x * y" instead. Basically inline copies it into the calling function. In this case, main. Think of it as pasting body of the function that was inlined, right into main. So anytime you make a call to the inlined function it''s as if the compiler pastes it right into where you''re calling it. The for inline functions is to try squeeze out extra performance so the compiler doesn''t have to make a jump which take a tiny bit more run time. Keep in mind though, that has a tendency to increase the size of the executable if you''re going to be making alot of calls to the inlined function. The reason is because it''s as if there are many copies of the function, instead of just one that is called when needed. Therefore it''s more ideal for smaller functions that aren''t call alot in the program. It''s kindof a balancing act and weighing out size vs. performance. Also keep in mind just because it''s inlined doesn''t mean it definitely will be inlined by the compiler (perhaps some exceptions though to this based upon compiler in question and options it has). It''s more of suggesting to the compiler to make the function inline. Generally the compiler decides.

Inlining tends to be more of one of those things for special cases, because most modern compilers are pretty good at optimizing. I suppose if you''re a programming wiz, you''d find situations where it pays off big to write code with optimizing tricks in mind. Anyway, I wouldn''t worry too much about stuff like that at this point. More that it''s good to know about it for later when you start looking for ways to tweak your code for extra speed out of it. Optimizing by hand is kindof an artform. I couldn''t tell ya much about it at this point. I''m probably about intermediate level with C++.

Both of these programming techniques, inline and recurvise functions, are probably something that will be more useful to you later on. Right now it''s probably just best to know about their existence and what the their general purpose is for.

I''m more and more convinced as time goes on, that programming is partly a science, and partly an artform. It''s like someone could code something one way, and it works fine, but someone else comes up with a different take on how to accomplish the same thing. Like you might make some code that doesn''t utilize recursive functions and it works great, while someone else makes a program to accomplish the same thing and they do use them. You will probably find tradeoffs to both approaches.

Anyway, hope that helps some. I too am still trying to make sense out of some of the programming concepts with C++, but I figured it might be good to see if I can offer some aid in trying to clarify what you wondering about. I know how hard it can be sometimes to make sense out of some of the concepts.

I''d recommend getting "C++ in 21 days" if you don''t have that book. That''s about the best book for teaching C++ I''ve seen so far. They described inline and resursive functions in way that was straight forward instead beating around the bush and confusing me.

http://www.maxcode.com/nuke/ has the book and you can download it free.
Advertisement
The fibbonacci sequence is the classic example for recursion, but it''s actually a pretty bad example, considering that there is an actual formula for the n''th term in that sequence.

The quicksort is a good example of recursion, although sorting techniques might be a bit advanced at your stage.

I really can''t think of any simple, good examples. Anyone else?

This topic is closed to new replies.

Advertisement