the inline function used to write a line of code. the inline function starts with inline keyword. you can write a simple code in an inlined function. inline function is an important feature in c++.
When you called Function by Normal Method then the pointer will jump on the function location and the first function will be executed. after complete execution Functions pointer will come back in the main program.
And When You use an inline function then You where declare an inline function, the compiler will automatically paste code on that place. inline function works faster and saves time. the inline function makes a easy to program. when you write more logic in the inline function then the compiler will ignore it. you can create variables and write some code and compile and check it.
Also Read This :- call by value and call by reference in C++
Benefits
Reduce function calling overhead
copy code resource in the program when you call the inline function
Speed high and reduces time compare to normal function.
Limitation
compile can not perform when the below code available in the program
if the function contains any loop (like for loop, while loop, do while loop)
if the function has a static variable
if the function is recursive
if the function return type is other than void
if the function has a Switch statement and Go to statement
Example of Inline function in c++
#include<iostream> using namespace std; inline int square (int n) { return n*n; } int main() { int x; cout<<"Enter Your Number = "<<endl; cin>>x; cout<<"square of "<<x<<" = "<<square(x); }
output
Enter Your Number = 2 square of 2 = 4