Inline function and find the maximum of two numbers in C++

Inline function and find the maximum of two given numbers in C++

In this tutorial, we will Learn:

What is an inline function in C++ with Programming example?

Write a C++ program to find the maximum of two numbers using inline function.

Solution:

Video Tutorial:

In normal function, First control will move from calling to called function.

Then arguments will be pushed on to the stack, then control will move back to the calling from called function.

This process takes extra time in executing. To avoid this, we use inline function.

An inline function is expanded whenever it is called.

That is, when we call the inline function the compiler replaces the call with the corresponding function definition, thus saving time of context switching between the calling and called function.

The following program demonstrates the C++ program to find the maximum of two numbers using the inline function.

 #include<iostream>
 using namespace std;
 inline int max(int x, int y)
 {
     if (x>y)
         return x;
     else 
         return y;
 }
 int main()
 {
     int a, b;
     cout<<"Enter the first number: ";
     cin>>a;
     cout<<"Enter the second number: ";
     cin>>b;
     cout<<"The maximum number is "<<max(a,b);
 }

OUTPUT:

See also  C++ Video Tutorial

Enter the first number: 30

Enter the second number: 25

The maximum number is 30

Summary:

In this article, we understood the concept of inline functions in c++ and Wrote a C++ program to find the maximum of two numbers using the inline functions.

If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

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