Raj Kumar
Computer Science And Engineering

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

Object Oriented Concepts

Explanation

1200    0
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:   
   Raj Kumar
Computer Science And Engineering

More Questions from Object Oriented Concepts Module 1