Inline Function in C++ Programming


The inline functions are a C++ enhancement feature to increase the execution time of a program. Functions can be instructed to compiler to make them inline so that compiler can replace those function definition wherever those are being called.

The program demonstrates the usage of the inline function in C++.

  • When a function is declared as "inline," the compiler replaces the function call with the actual function code at the point where the function is called, instead of jumping to the function's location in memory.
  • In this program, the cube() function is declared as an inline function using the inline keyword. This function takes an integer argument and returns the cube of the argument. The main() function calls the cube() function with the value 2 and prints the result.
  • Since the cube() function is declared as inline, the compiler replaces the function call with the function's code at the point where the function is called. This reduces the function call overhead, making the program more efficient.

Source Code

//Inline Function in C++ Programming
#include<iostream>
using namespace std;
inline int cube(int x)
{
    int result = x*x*x;
    return result;
}
int main()
{
    int x=2;
    cout<<"Cube is  : "<<cube(x);
    return 0;
}
 
 

Output

Cube is  : 8
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples