Inline Function in C

A normal function will become an inline function when the function prototype of that function is prepended with the keyword “inline”. Inline functions are the function where definitions are small and substituted when the function call occurs. Function substitution is totally a compiler’s choice. The property of the inline function is that the compiler inserts the entire body of the function in the place where inline function name is used in the program.

The main reason for making a function inline is to hint the compiler that it is worth making extra effort to call a function faster than otherwise; generally, by substituting the function’s code into its caller. It eliminates the need for a call and a return request sequence and allows the compiler to perform certain optimizations between the bodies of both functions. Sometimes it will be necessary for the compiler to emit a stand-alone copy of an object code for the function even if it is an inline function, such as if it is necessary to take the address of a function or it cannot be inlined in a particular context or if the optimization has been turned off.

An inline function of any kind can emit stand-alone abject code, or not emit stand-alone object code, or only emit stand-alone object code if it is required. Sometimes an inline function can lead to duplication of object code and will become a massive problem for some reasons such as:

  • It leads to wastage of space.
  • It can cause the pointers to compare one another, which may or may not be equal.
  • It might reduce the effectiveness of the instruction cache. However, inlining might do the same in different ways too.

Syntax:

E.g.:

Output:

Inline rules:

  • This is a function where all the declaration, including the definition, mentions inline and never mentions an extern. There has to be a definition in the same translation unit. The C programming standard refers to this as an inline definition. A program can have a separate definition that is not an inline function in the unit, and the compiler can choose either that or the inline function. Functions may not contain modifiable static variables and may not refer to static functions or variables where they are declared.

E.g.:

            Here, the function will not be callable from other files.

  • The function where atleast one declaration is inline, but other declaration does not mention inline or extern. There must be a definition in the translation unit. It works as a normal function and can be allied from other translation units present in the program.

E.g.:

Here, the functions are callable from other files.

  • The function is said to be a static inline. A local definition may be emitted if and only if required. A programmer can have multiple definitions in the program, but still, it works in the same way. Inlining a function reduces the program to a portable one. This will be useful primarily for the small function that uses macros. If the function is not always inlined, then you might get duplicates of the object code. Hence the solution for this will be including static inline function in either of the header file id they are to be widely used or just in the source file that uses them.

E.g.:

Output:

Advantages:

  • Inline functions can include multiple lines of code without trailing backslashes.
  • Debugging a code is accessible in inline when compared to macros.
  • The main advantage of using an inline function is that it does not require the function call and does not return anything from the function.
  • They have their scope of variables and can return a value.
  • Since inline are functions, the compiler’s type of arguments is checked whether they are right or wrong.
  • No risk is involved in it is called multiple times. But it might be present in macros which is dangerous when the argument is an expression.

Disadvantages:

  • It increases the file size just as much as the function code is copied repeatedly in the program whenever it is called.s

Pin It on Pinterest

Share This