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

Inline Function

Inline functions in C++ are expanded in-line during invocation, enhancing execution speed by eliminating function call overhead. They are defined using the 'inline' keyword and must be declared before use, but may not be effective for larger functions or those with complex structures. Macros, defined with #define, differ from inline functions as they are replaced by code fragments without type checking, while inline functions provide better type safety and can be defined anywhere in the program.
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 views

Inline Function

Inline functions in C++ are expanded in-line during invocation, enhancing execution speed by eliminating function call overhead. They are defined using the 'inline' keyword and must be declared before use, but may not be effective for larger functions or those with complex structures. Macros, defined with #define, differ from inline functions as they are replaced by code fragments without type checking, while inline functions provide better type safety and can be defined anywhere in the program.
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
You are on page 1/ 5

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
Question: Write an Inline function in C++ that calculates and prints the volume of
a sphere. Use an inline function sphereVolume that returns the result of the
following expression: (4.0 / 3.0 * 3.14 * pow (radius, 3)).

Ans:
#include<iostream>
#include<cmath>
using namespace std;
inline double sphereVolume(double radius)
{
double PI=3.14,sVol;

sVol=((4.0 / 3.0) * PI * pow(radius, 3));

return sVol;

int main()
{
double radius;
cout << "Input the radius of the sphere: ";
cin >> radius;
cout << "\nThe volume of the sphere is " << sphereVolume(radius)
<< endl;

return 0;
}

3
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;
}

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);

4
return 0;
}

Difference between macro and inline function

Sr. Macro Inline function


No
1. Macro is a fragment of code, An inline function is a C++ enhancement
which is preprocessor directive feature to minimize the execution time of
that is included at the a program.
beginning of the program
preceded by a hash sign.
Evaluation Time
2. In macro, the arguments are In inline function, the argument is
evaluated every time whenever evaluated once.
macro is used in the program.
Keyword
3. Macro use #define Inline function use the keyword “inline”
Usages
4. Macro is used to define An inline function is used to minimize the
constants, expression, and to execution time of a program.
define function etc
Termination
5. Macro terminates with the new Inline function terminates with the curly
line. brace at the end of the inline function.
Defining point
6. Macro is defined at the begning Inline function can be defined inside or
of the program. outside the class.

You might also like