0% found this document useful (0 votes)
5 views4 pages

Understanding Inline Functions in C++

Inline functions are expanded in-line during invocation, improving execution speed by eliminating function call overhead, but they require more memory as they are copied at each call site. The compiler may ignore the inline request for complex functions, and inline functions have limitations such as not working with control structures or recursion. Macros, defined using #define, replace code snippets but lack type checking, making inline functions a safer alternative with better error handling.

Uploaded by

Muskan Soni
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)
5 views4 pages

Understanding Inline Functions in C++

Inline functions are expanded in-line during invocation, improving execution speed by eliminating function call overhead, but they require more memory as they are copied at each call site. The compiler may ignore the inline request for complex functions, and inline functions have limitations such as not working with control structures or recursion. Macros, defined using #define, replace code snippets but lack type checking, making inline functions a safer alternative with better error handling.

Uploaded by

Muskan Soni
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()
{ In inline function compiler replaces the function call with
the corresponding function code rather than calling the
cout<<(10>20?10:20); function definition.
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.

#include<iostream>
using namespace std;
#define SQUARE(j) j*j

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
Macros
 A macro is a piece of code in a program that is replaced by the value of the
macro.
 Macro is defined by #define directive.
 Whenever a macro name is encountered by the compiler, it replaces the
name with the definition of the macro.
 Macro definitions need not be terminated by a semi-colon(;).

Object-like Macros:

#include<iostream>
using namespace std;
#define LIMIT 5

// Driver Code
int main()
{
// Print the value of macro defined
cout << "The value of LIMIT is " << LIMIT;

return 0;
}

3
Function-like Macro:
#include <iostream>
using namespace std;
#define PI 3.1416
#define AREA(r) (PI*(r)*(r))

int main() {

float r = 7; // radius of circle

cout<<"Area of Circle with radius " << r <<": "<< AREA(r);

return 0;
}

You might also like