# inline function

#### Kittu20

Joined Oct 12, 2022
199
I have seen both the below links to understand difference between inline function and normal function

https://www.geeksforgeeks.org/inline-function-in-c/
https://www.tutorialspoint.com/what-is-an-inline-function-in-c-language

I have created one normal function "add" and one inline function "sum" I don't see any difference when program execute

C:
#include<stdio.h>
int add( int x, int y)
{
int result = x + y;
return result;
}
// I am using GCC compiler
static inline int sum( int x, int y)
{
int result = x + y;
return result;
}

int main()
{
int a = add ( 2, 5);
printf(" a : %d \n", a);
int b = sum( 2, 5);
printf(" b : %d \n", b);
return 0;
}
Output
Code:
 a : 7
b : 7
can anyone show me how inline function is different as compare to normal function ?

#### WBahn

Joined Mar 31, 2012
28,138
How are you trying to determine if there is a difference?

Functionally, there better not be any difference.

The difference is, potentially, in the performance/space tradeoff.

The 'inline' qualifier is a suggestion/hint/plea to the compiler to replace all calls to the function with the body of the function. This eliminates the function call overhead and can result in significant speed up in execution if it's a function that's called lots and lots of times (like within the body of a loop that gets executed millions of times). But it generally results in increased code size, particularly if it's a function that's called in many different places.

The compiler is not required to actually inline a function just because the programmer requests it. The compiler gets to decide whether or not it meets its criteria for inlining it. Some simple compilers just ignore the inline qualifier outright.

#### dl324

Joined Mar 30, 2015
15,510
If you want to force a function to be inline regardless of what the compiler wants to do, use a macro instead of the inline directive.

Parenthesis might be required in some cases. If you don't want to think about them, just use them all the time.

From K&R Second Edition:

#### Kittu20

Joined Oct 12, 2022
199
[
The 'inline' qualifier is a suggestion/hint/plea to the compiler to replace all calls to the function with the body of the function. This eliminates the function call overhead
I am trying to understand why inline functions are used and what is the purpose of inline function?

After reading your post i get idea that the execution time of inline function is less than normal function. It also increases the size of application.

#### WBahn

Joined Mar 31, 2012
28,138
[

I am trying to understand why inline functions are used and what is the purpose of inline function?

After reading your post i get idea that the execution time of inline function is less than normal function. It also increases the size of application.

#### Papabravo

Joined Feb 24, 2006
19,822
[

I am trying to understand why inline functions are used and what is the purpose of inline function?

After reading your post i get idea that the execution time of inline function is less than normal function. It also increases the size of application.
Yes, and not necessarily.
The execution time should be less because there is no subroutine call, saving of registers on the stack, restoring registers from the stack and a subroutine return.

When it comes to space, there might actually be a savings in space, especially if the inline function can be accomplished with a single instruction on register variables. So, no need for an explicit call with a 32-bit or 64-bit address associated with the subroutine call. This will vary with the target hardware and the methods available for calls and returns. It is possible with a short instruction form subroutine call that there will be no space savings. Given the negligible cost and the wide availability of large memories, nobody really cares about optimizing for space any more.

The only way to accurately verify this is to look at the generated machine code. You cannot do it from the program output, you must get an assembly language listing of the compiler output or look at a disassembly of the actual code in a debugger.

It is extraordinarily naive to assume that you can draw conclusions about such propositions without evidence to back up those assumptions.