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


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:
READ Wipro ELITE National Talent Hunt Hiring 2021 Enter the first number: 30 Enter the second number: 25 The maximum number is 30


Share to whatsapp

More Questions from Object Oriented Concepts Module 1

What are the basic differences between the POP (Procedure Oriented Programming) and OOP (Object Oriented Programming)?
View
What is an inline function? Write a C++ program to find the maximum of two numbers using inline function.
View
Static member and count the number of objects in C++
View
Basic concepts (features) of Object-Oriented Programming C++
View
Friend function in C++ with Programming example
View
Function Overloading with an Example in C++
View