One of the main objectives of using functions in a program is to save time and memory space (by avoiding code repetition) which becomes appreciable when a function is likely to be called many times.

If we use inline functions, the compiler replaces the function call with the corresponding function code similar to macros.

Inline functions are similar to macros. However, they provide better type checking & deal well with macros.

The syntax for inline functions:

inline <return value-type> <function name> (parameter list) { function body }

Few more points on inline functions

  • If functions are defined within the class, then they are automatically inline (implicit inline functions).
  • Inline functions are best used for small frequently used functions.
  • All inline functions must be defined before the functions call because the C++ Compiler has to put the code in the function body directly inside the code in the calling program.

Some compilers treat an inline function as a normal function if the inline function definition constants:

  • loops
  • switch-case
  • recursion
  • static variable etc.

Inline Function in C++ with Example program

#include<iostream>
using namespace std;
inline float sum(float a, float b)
{ return a+b;
} 
int main(){ 
float s;
s = sum(4.6f, 9.2f);
cout<<"The sum is"<<s<<endl;
return 0; }

Output:-The sum is :13.8

Explanation of the program

In this program, sum() is the inline function. During execution, the value of a becomes 4.6 and the value of b becomes 9.2.

The of the 2 variables is the value of s since that is the value returned by the sum() function.

This article is written by our awesome writer
Comments to: Inline Function in C++ with Example

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

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password