Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are inline functions?
3.
Advantage of inline functions
4.
Disadvantage of inline functions
5.
When not to use inline functions
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Inline functions

Author Urwashi Priya
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Whenever a function is called, then control goes to the function definition. All the statements in the function definition are executed. Once the function definition execution is over, then the control goes to the calling function.

Say, for example
main() {
	add();
	…
}

add() {
	…
}

 

In the main function, the first statement is a call to add a function. The control goes to add function and executes it. After executing add function, the control goes back to the statement in the main function, next to where add function was called. 

Whenever a function call occurs, the CPU stores the address of the very next statement in the stack. Next control will be transferred to function definition, and the statements are executed. Next, the control is transferred back from the called function to the calling function. Now, this process has included 3 steps, CPU takes 3 units of time. Now let’s assume that function definition includes only two statements and takes only 2 units of time. So just handling the program takes more time than actually performing the operation. This becomes somewhat tedious.

So we bring inline functions into the picture.

Also see, Literals in C, Fibonacci Series in C++

What are inline functions?

We use inline functions in the above-mentioned cases. Inline function is expanded inline when it is invoked. Whenever an inline function is called, the function definition is substituted or inserted at the function call time. Inline function is just another name for replacement function instead of calling it.

Example:

#include <iostream>
#include <stdio.h>
using namespace std;

inline int max(int a, int b){
    return a>b?a:b;
}

int main()
{
    int a=10, b=20;
    cout<<"maximum of 10 and 20 is "<<max(a,b);
    return 0;
}

 

max(a, b) denotes function call and gets replaced by return a>b? a:b.

Output:

Sample Output:
maximum of 10 and 20 is 20

 

You can try and compile with the help of online c++ compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Advantage of inline functions

  • The inline function just replaces the statement, no need to move from calling function to called function and vice versa.
  • It saves memory space reduces time complexity.
  • It can be used as a substitute for macros.
  • It does not require any extra data structure such as stack and thus saves the rigorous push and pop operation.

Disadvantage of inline functions

  • Sometimes the speed of instruction is reduced due to multiple uses of inline functions.
  • Compile-time increases due to multiple uses of inline functions.

When not to use inline functions

  • Inline function is not used if the function definition is quite large.
  • These functions are not to be used if a loop, switch statement exists.
  • Also, in the case of static variables, do not use inline functions.

FAQs

  1. What are macros?
    macros are defined by #define directive. It replaces the name of macro with the value of the macro. For Example: “# define a 10”, a is the name of the macro and wherever a exists in the program it is replaced by 10.
     
  2. What is the difference between inline and normal function? 
    The inline function reduces the time taken for an algorithm to occur by just replacing the calling function. Normal functions are used for the reusability of the program.
     
  3. What is the difference between macro and inline?
    Inline function is a short function that is analysed by the compiler, whereas the preprocessor expands a macro.

Key Takeaways

This article taught us about inline functions. 

We hope you could easily take away all critical and conceptual techniques by walking over the given examples. 

Now, we strongly recommend you to understand the other related concepts in C++ and enhance your learning. You can get a wide range of topics similar to this on basics of c++

It's not the end. Learn and explore more.

Previous article
Getline Function in C++
Next article
seekg() Function C++
Live masterclass