Inline function

What is an inline function? What its duty? when we can use it? kindly show me a simple code.
An inline function will more or less replace the function call in your code with the actual function code when compiled. That is to say, it'll insert the function code into your code stream.

They can improve performance when used correctly.

Using them is no different to a regular function, other than using the inline keyword when you define them.
Last edited on
Your example:

1
2
3
4
5
6
7
8
9
10
inline int add(int a,int b)
{
    return(a*b);
}

int main()
{
      ...
      add(x,y); //call it normally without the inline keyword.
}
When using inline can you still use the variable names which are local variable defined in a function? e.g. If you define one in main then refine a variable in your function using the same name will it cause problems if you use inline?
It's slightly more accurate to say that the 'inline' command suggests to the compiler that you want the code from the function inserted into the location of the file that it is called. There is NO GUARANTEE that the compiler will honor the command.

In case you are wondering when you would use this, generally you don't because it's not something most programmers need to worry about. In C++ when you call a function the processor stores the current address held by the stack pointer and the stack base pointer registers into memory along with the data you plan on passing to the function. Then it loads the address of that function into those registers and zeros out the (logic?)* flags all before it even starts to process the function. Modern computers can do all of this a hundred and one times before you can blink, but in the rare instance when you need to preserve that fraction of a second or you are running on hardware that might not have spare room in the stack you would consider using the 'inline' command.

*: I can't remember what these flags are referred to as, I took a break from reverse engineering when I found out that GCC and therefore MingW as well will compile the same code completely differently each time to prevent people from stealing it.
Last edited on
Thank you.
Topic archived. No new replies allowed.