inline function and normal function – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sat, 05 Sep 2020 13:56:53 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg inline function and normal function – Programmerbay https://programmerbay.com 32 32 Difference between Inline function and Normal function https://programmerbay.com/difference-between-inline-function-and-normal-function/ https://programmerbay.com/difference-between-inline-function-and-normal-function/#respond Sat, 03 Aug 2019 16:36:30 +0000 https://www.programmerbay.com/?p=4464 A function is a piece of code that is used to perform a specific task. It reduces the complexity and improves the readability of the code.

In  C++, a concept named inline function comes into play where a function call is replaced with its function definition by the compiler

Difference between Inline Function and Normal Function

1. Syntax

The syntax of an inline function and a normal function is the same except that inline function has the keyword “inline” in its definition.

Inline Function:

#include <iostream>
using namespace std;
inline int square(int s)
{
     return s*s;
}
void main()
{
     cout << square(5) << "\n";
}

Normal Function:

#include <iostream>
using namespace std;
int square(int s)
{
     return s*s;
}
void main()
{
     cout << square(5) << "\n";
}

 

2. Execution

At the time of program execution, the code is compiled first and then after converted to executable code.

If in that executable code any function call is found, the compiler stores the address of the instruction immediately and jumps to its associated function definition.  Then after, the compiler executes the function code and gets back to the stored instruction address after the function call.

In inline function, the compiler replaces the function call with the function definition code and compiles the entire code. This process is called expansion.

3. Overhead

The normal function creates overheads as and when a function call encounters.

An inline function does not create function call overhead as the function definition code is replaced in the program.

4. Memory Usage

Inline function in the repeated calls such as in a loop wastes memory but save CPU time to execute that call.

Normal function in such calls does not waste memory but uses more CPU time to execute that call.

5. Function call Time

In normal functions which have small size of code, the time needed to make the function call’s jumps is more than the time needed to execute the function’s code.

In the inline function, no such case occurs.

6. Too many Inline functions

Using too many inline functions increases the executable file size because of the duplication of the same code.

Using too many normal functions does not affect the file size after compilation.

7. Inline function in a class

All functions inside a class in C++ are implicitly inline.

All functions outside a class in C++ are normal functions.

More about virtual functions

Virtual functions are not inline because the call to virtual function is resolved at the run time but inline functions call is resolved at compile time.

]]>
https://programmerbay.com/difference-between-inline-function-and-normal-function/feed/ 0