Inline Functions

C++ provides inline functions to reduce the function call overhead. An inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of the inline function call. This substitution is performed by the C++ compiler at compile time. An inline function may increase efficiency if it is small.

To create an inline function, we use the inline keyword.
For example

inline returnType functionName(parameters) 
{
//code
}

The compiler may not perform inlining in such circumstances as: 

  1. If a function contains a loop. (for, while and do-while
  2. If a function contains static variables. 
  3. If a function is recursive. 
  4. If a function return type is other than void, and the return statement doesn’t exist in a function body. 
  5. If a function contains a switch or goto statement.

Program:

#include <iostream>
using namespace std;
// Inline function definition
inline int square(int x) {
    return x * x;
}

int main() {
    int num;
    // Taking input from the user
    cout << "Enter a number: ";
    cin >> num;
    // Using the inline function
    cout << "The square of " << num << " is: " << square(num) << endl;
    return 0;
}

Output:
Enter a number: 22
The square of 22 is: 484

Process returned 0 (0x0)   execution time : 11.213 s
Press any key to continue.


Inline Function for Maximum of Two Numbers

#include <iostream>
using namespace std;

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

int main() {
    int num1 = 10, num2 = 20;
    cout << "The maximum of " << num1 << " and " << num2 << " is: " << getMax(num1, num2) << endl;
    return 0;
}

Output:

The maximum of 10 and 20 is: 20

Process returned 0 (0x0)   execution time : 3.002 s
Press any key to continue.


Program:

#include <iostream>
using namespace std;

class operation {
    int a, b, add, sub, mul;
    float div;

public:
    void get();
    void sum();
    void difference();
    void product();
    void division();
};
inline void operation ::get()
{
    cout << "Enter first value:";
    cin >> a;
    cout << "Enter second value:";
    cin >> b;
}

inline void operation ::sum()
{
    add = a + b;
    cout << "Addition of two numbers: " << a + b << "\n";
}

inline void operation ::difference()
{
    sub = a - b;
    cout << "Difference of two numbers: " << a - b << "\n";
}

inline void operation ::product()
{
    mul = a * b;
    cout << "Product of two numbers: " << a * b << "\n";
}

inline void operation ::division()
{
    div = a / b;
    cout << "Division of two numbers: " << a / b << "\n";
}

int main() {
    cout << "Program using inline function\n";
    operation s;
    s.get();
    s.sum();
    s.difference();
    s.product();
    s.division();
    return 0;
}

Output:

Program using inline function
Enter first value:50
Enter second value:10
Addition of two numbers: 60
Difference of two numbers: 40
Product of two numbers: 500
Division of two numbers: 5

Process returned 0 (0x0)   execution time : 20.078 s
Press any key to continue.



No comments:

Post a Comment