0% found this document useful (0 votes)
10 views

Inlinefunction

Uploaded by

himanshubaba1406
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Inlinefunction

Uploaded by

himanshubaba1406
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

What is inline function?

An inline function is a combination of macro & function. At the time of


declaration or definition, function name is preceded by word inline.
When inline functions are used, the overhead of function call is
eliminated. Instead, the executable statements of the function are copied at
the place of each function call. This is done by the compiler.
When the function is defined Inline, the C++ compiler puts the function body
inside the calling function. You can define function as Inline when the function
body is small and need to be called many times, thus reduces the overhead in
calling a function like passing values, passing control, returning values,
returning control.

Example

#include<iostream>
using namespace std;

inline int cubedata(int a)


{
return a*a*a;
}

inline float areatri(float bs,float ht)


{
return ((bs*ht)/2);
}

int main()
{
int num,res;
float bs,ht,ar;
cout<<"Enter Number For Cube ";
cin>>num;
res=cubedata(num);
cout<<"cube of number is "<<res<<endl;
cout<<"Enter base and height ";
cin>>bs>>ht;
ar=areatri(bs,ht);
cout<<"Area Of Triangle is "<<ar<<endl;
return 0;
}

Some of the situations where inline expansion may not work are:
(1) If Inline Functions are recursive.

(2) If functions conation static variables.

(3) For functions not returning values, if a return statement exists.

(4)The compiler is unable to perform inlining if the function is too


complicated. So we must avoid big looping conditions inside such functions.
In case of inline functions, entire function body is inserted in place of each
call, so if the function is large it will affect speed and memory badly.

You might also like