What Are Inline Functions In C?

What are inline functions in C?

What are functions in C programming? Read Below

 

Before learning what inline functions are in C, let’s revise what functions are in C.

Functions are a set of instructions that are executed together and aim at performing a specific, well-defined task. They are contained neatly within a block of code and are used to make the code modular and reusable. If you wish to learn more about the importance of C, then check out our article on “How To Start Learning C In 5 Easy Steps?“.

An example of a function can be the following code that adds two integers:

Code:

 

#include <stdio.h>
// Normal function int add(int a, int b)

{

return a+b;

}

// Main function int main()

{

int ans = add(2,3); printf("%d", ans); return 0;

}
Output:

5

 

Now, whenever we need to add two integers, we can call the function, which will then calculate the sum of the two integers and return the answer.

Functions can also contain complex instructions with larger lines of code.

 

What are inline functions in C programming? Read Below

 

An inline function is similar to a function in C, but instead of calling the function to execute the task, the compiler simply copies the body of the inline function wherever it is called. It hence gets its name as “inline” since it just copies the block of code in the same place.

An inline function in C is a compiler powerful optimization technique that can significantly impact the program’s performance. By declaring a function as inline, we request the compiler to replace function calls with the actual function code at the call site. However, the final decision to copy the code of the inline function depends on the compiler itself.

We can declare a function as inline by just writing the “inline” keyword before the function declaration.

When should we use inline functions in C?

 

Inline functions are used to save the overhead costs that occur when calling a normal function.

Whenever a function is called, all its data is put in a stack frame which is then pushed on top of the program stack. Pointers and registers are updated every time a function is called and after it is executed completely.

These overheads often prove to be inefficient and time-consuming for very simple functions.

Inline functions save all of these overhead costs by simply copying the block of code and then proceeding with the current function execution normally.

Inline functions are thus used for very simple functions typically written in a few lines. They optimize the execution time of these functions and execute faster.

 

When should we use inline functions in C?

 

However, too many inline functions within a code are generally avoided since they simply copy the code everywhere, leading to larger disk space needed by programs, also considering the fact that these are harder to debug and test than normal functions.

We will rewrite the above program to use as an inline function:

Code:

 

#include <stdio.h>




// inline function

inline int add(int a, int b)

{

return a+b;

}




// Main function int main()

{

int ans = add(2,3); printf("%d", ans); return 0;

}
Output:

5

In the above program, we use an inline function to add two integers. In the previous code, whenever the add function was called, the code execution would shift to the add function.

However, in this case, the code enclosed within the ‘add’ inline function will be copied into the main function where it is called. So, instead of creating a new stack frame and pushing it onto the program stack, we get the code copied from the inline function into the main function and save all the overhead costs.

A word of caution when using inline functions :

 

When using certain compilers, we may encounter a linkage error. You may see something like: undefined reference to `add’

This error typically occurs during the linking phase of the compilation process when the linker cannot find a definition for a symbol (in our case, the inline function ‘add’) that is being referenced.

This happens because the compiler copies the code of the inline function in the current function, and no separate function call is made to the ‘add’ function.

Also, if you’re curious to know about errors in C, then you can check out our article on “What Are Errors In C Programming?

However, since the linker’s job is to resolve the symbols with their definitions, when it can’t find a separate definition of the function ‘add’, it throws the error mentioned above.

One may only encounter this error on some compilers, and it completely depends on how the compiler handles the inline functions.

To save us from the error, we can declare the inline function as ‘static’. By adding static, the inline function becomes locally defined within the source file, and its definition becomes visible to the linker.

Code:

 

#include <stdio.h>




// declaring inline function as static static inline int add(int a, int b)

{

return a+b;

}




// Main function int main()

{

int ans = add(2,3); printf("%d", ans); return 0;

}
Output:

5

So, hope you have liked this piece of article. Share your thoughts in the comments section and let us know if we can improve more.

Also, if you guys are looking to gain more knowledge about the concept of  C then you check out our below articles:

If you still have some doubts or issues regarding C homework help then you can contact us and hire CodingZap experts.

Leave a Comment

Your email address will not be published. Required fields are marked *