Question:
Inline functions?? (c++)?
GGP
2009-07-08 03:05:41 UTC
I know that an inline function is a function where its body is inserted at every point the function is called, instead of regular calls... I know that it should be used with functions having short bodies only otherwise it wont be as fast.

What i dont know is what are its uses?? Sounds to me its the exact same thing as a normal function..

Also what are the uses of recursive functions?
Five answers:
binesh
2009-07-08 03:24:13 UTC
You have already mentioned it's uses. The difference it produces is in the execution time of the program. That is, if a small function is called many a times in a program then, each time the control has to jump and then come back to the calling program,thereby consuming much time, just for the execution of few lines of code.



If such a small function that is called repeatedly, is defined within the program(by the compiler itself), the time consuming jump can be avoided thereby,improving the response time!



Now,if the called function is too long then, defining it is inline would make the calling program too long and you won't get the benefit of improving the execution time.



Also,if the function is to be called just once,defining it as inline won't be a wise decision either!





Recursive functions is useful when you require the same function to perform the task meant for that function.



Following is the factorial function,finding the factorial using recursion.



fact(int i)

{

if(i>1)

f=i*fact(i-1);

}
2016-04-05 02:50:57 UTC
"inline" functions can be considered compiler macros. That is, the code that the function executes replaces the function at the point in the program where the function is invoked during compilation of the code. The pro is much faster execution with no use of stacks (and transfers of execution focus) [ no passing of arguments, no pushing of program state to stack, both of which require additional cpu time ] The con is each place where the function is invoked has additional code added, increasing program size. In many modern applications, memory is cheap and speed is one of the important aspects of program design. Modern compilers will often choose to replace functions with the inline code for speed optimization. Certain functions, for instance recursive functions, can NOT be inline. In general, specifying a function to be inline should be limited to those functions which have 'few' lines of code.
Oliver
2009-07-08 03:35:33 UTC
Inline functions are used for making code simpler (less repeating code) for the programmer and are more a bit more efficient than a normal function call (they don't have to push the parameters onto the stack then call the function which takes time). Your compiler probably decides for itself to inline some functions which aren't declared as inline functions as a part of its optimization, as if you were to call a function as many times as you can per second then you would be better off removing the overhead of pushing the parameters and calling the function which take precious cycles. In the way that they help make code look cleaner they try to replace #defines e.g.



#define ROTR(x,n) ( (x>>n)|(x<<(sizeof(x)-n)) )



inline int ROTR(int x, int n){

return ( (x>>n)|(x<<(sizeof(x)-n)) );

}



attached is a link on inline functions from cplusplus.com



A recursive function is a function which calls itself, these can make some tasks easier for the programmer such as traversing a tree structure. Also linked is the wikipedia article on them which is a bit more thorough than a Y!A
AnalProgrammer
2009-07-08 03:18:17 UTC
Inline functions have nothing to do with speed. They are functions and do the same thing that a normal function will do in terms of execution. The only thing about keeping an inline function short is that it becomes messy code if it is too long!



A recursive function is a function that calls itself.

Recursive functions can be used instead of a loop in a program.



Have fun.
Youn
2009-07-08 03:23:46 UTC
not exactly the same thing as a normal function. there isnt the function call overhead... which may be problematic if you're repeating the code block 1 million times.



An example for use would be a look up function, simple database abstraction function calls... the advantage is that you dont get the function overhead, yet you can optimize the code block and it eases maintainance (especially if this particular code is used many times).



Optimization is either in speed or in size... using inline functions will yield a larger program.... but sometimes it can be faster


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...