0% found this document useful (0 votes)
2 views3 pages

Inline Function

An inline function is expanded in-line during invocation, replacing the function call with its code to enhance execution speed. It is defined using the 'inline' keyword and must be declared before use, but may increase memory usage as the function code is copied at each call. However, the compiler may ignore the inline request for complex functions, and inline functions are distinct from macros as they provide better type checking.

Uploaded by

Avishka Pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Inline Function

An inline function is expanded in-line during invocation, replacing the function call with its code to enhance execution speed. It is defined using the 'inline' keyword and must be declared before use, but may increase memory usage as the function code is copied at each call. However, the compiler may ignore the inline request for complex functions, and inline functions are distinct from macros as they provide better type checking.

Uploaded by

Avishka Pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Inline Function

Inline function is a function that is expanded in-line when it is invoked. That is the compiler replaces the
function call with the corresponding function code rather than calling the function definition.

To make a function inline, we need to prefix the keyword “inline” to the function definition. All inline
function must be defined before they are called.

Syntax:
<inline> <return-type> <function-name>
{
Statement _1;
Statement _2;
.
.
Statement _n;

#include<iostream>
using namespace std;

inline int max(int a,int b)


{
return (a>b?a:b);
}

int main()
{

cout<<max(10,20);

return 0;
}

As far as compiler is concerned, the program is equivalent to the below one.

main()
{

cout<<(10>20?10:20);
return 0;

1
Inline mechanism increases the execution performance in terms of speed. The overhead of repetitive
functions call and returning values are removed. The inline function is mostly useful when calling
function for only small programs. The speed benefits of inline function diminish/removed as the function
grows in size.

On the other hand, the program using inline functions need more memory space since the inline functions
are copied at every point where the function is invoked.

Note: Inline function simply sends a request not a command to the compiler. The compiler may ignore
this request if the function definition is too long or too complicated and compile the function as a normal
function.

Some of the situations where inline function may not work are:

1. If function containing control structure statements such as if, switch, for loop etc.
2. If functions are recursive
3. If function contain static variable
4. The main() function cannot work as inline.

The inline functions are similar to macro of C programming language. The main limitation with macro is
that they are not functions and errors are not checked at the time of compilation. The function offers
better type testing and do not contain any side effects as present in macro.

inline int square(int j)


{
return (j*j);
}

int main()
{

int p=3,q=3,r,s;
r=SQUARE(++p);
s=square(++q);
cout<<endl<<"r="<<r<<endl;
cout<<"s="<<s;

return 0;
}

Output:

2
3

You might also like