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

Inline Functions in C++

Uploaded by

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

Inline Functions in C++

Uploaded by

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

FACULTY OF ELECTONICS TECHNOLOGY

Subject: CP II SEMESTER 4th


Inline Functions in C++
Inline Functions in C++
C++ provides inline functions to reduce the function call overhead. An inline
function is a function that is expanded in line when it is called. When the inline
function is called whole code of the inline function gets inserted or substituted at
the point of the inline function call. This substitution is performed by the C++
compiler at compile time. An inline function may increase efficiency if it is small.
inline return-type function-name(parameters)
{
// function code
}

 Remember, inlining is only a request to the compiler, not a command. The


compiler can ignore the request for inlining.
The compiler may not perform inlining in such circumstances as:
1. If a function contains a loop. (for, while and do-while)
2. If a function contains static variables.
3. If a function is recursive.
4. If a function return type is other than void, and the return statement doesn’t
exist in a function body.
5. If a function contains a switch or goto statement.
Inline functions Advantages:
1. Function call overhead doesn’t occur.
2. It also saves the overhead of push/pop variables on the stack when a function
is called.
3. It also saves the overhead of a return call from a function.
4. When you inline a function, you may enable the compiler to perform
context-specific optimization on the body of the function. Such
optimizations are not possible for normal function calls. Other optimizations
can be obtained by considering the flows of the calling context and the
called context.
5. An inline function may be useful (if it is small) for embedded systems
because inline can yield less code than the function called preamble and
return.
Inline function Disadvantages:
1. The added variables from the inlined function consume additional registers,
After the in-lining function if the variable number which is going to use the
register increases then they may create overhead on register variable

1
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Inline that
resource utilization. This means Functions
when theininline
C++ function body is
substituted at the point of the function call, the total number of variables
used by the function also gets inserted. So the number of registers going to
be used for the variables will also get increased. So if after function inlining
variable numbers increase drastically then it would surely cause overhead on
register utilization.
2. If you use too many inline functions then the size of the binary executable
file will be large, because of the duplication of the same code.
3. Too much inlining can also reduce your instruction cache hit rate, thus
reducing the speed of instruction fetch from that of cache memory to that of
primary memory.
4. The inline function may increase compile time overhead if someone changes
the code inside the inline function then all the calling location has to be
recompiled because the compiler would be required to replace all the code
once again to reflect the changes, otherwise it will continue with old
functionality.
5. Inline functions may not be useful for many embedded systems. Because in
embedded systems code size is more important than speed.
6. Inline functions might cause thrashing because inlining might increase the
size of the binary executable file. Thrashing in memory causes the
performance of the computer to degrade. The following program
demonstrates the use of the inline function.
Example:
#include <iostream>
using namespace std;
inline int cube(int s) { return s * s * s; }
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0

Another Example of inline function


#include <iostream>
#include<cmath>
using namespace std;
// define an inline function
// to calculate square root of a number
inline float squareRoot(int num)
{
return sqrt(num);
2
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
} Inline Functions in C++
// define an inline function
// to calculate square of a number
inline int square(int num)
{
return num * num;
}
int main()
{
int num = 9;
cout << "The square root of " << num << " is: ";
// calling inline function squareRoot()
cout << squareRoot(num) << "\n";
cout << "The square of " << num << " is: ";
// calling inline function square()
cout << square(num) << "\n\n";
return 0;
}

Inline Function and Classes

C++ also allows you to declare the inline functions within a class. These functions
need not be explicitly defined as inline as they are, by default, treated as inline
functions. All the features of inline functions also apply to these functions.
However, if you want to explicitly define the function as inline, you can easily do
that too. You just have to declare the function inside the class as a normal function
and define it outside the class as an inline function using the inline keyword.

#include <iostream>
using namespace std;
// a class to find the area
class findArea
{
public:
// declare functions
// (implicitly inline functions)
void circle(int radius);
void square(int side);
3
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
Inline Functions in C++
void triangle(int base, int height);
void rectangle(int length, int breadth);
};

// define the inline functions


// outside the class, using the
// scope resolution operator (i.e. ::)
// function to find the area of a circle

inline void findArea :: circle(int radius)


{
cout << "The area of the circle is: ";
cout << (3.14 * radius * radius) << "\n";

// function to find the area of a square


inline void findArea :: square(int side)
{
cout << "The area of the square is: ";
cout << (side * side) << "\n";
}

// function to find the area of a triangle


inline void findArea :: triangle(int base, int height)
{
cout << "The area of the triangle is: ";
cout << (0.5 * base * height) << "\n";
}

// function to find the area of a rectangle


inline void findArea :: rectangle(int length, int breadth)
{
cout << "The area of the rectangle is: ";
cout << (length * breadth) << "\n";

int main()

4
FACULTY OF ELECTONICS TECHNOLOGY
Subject: CP II SEMESTER 4th
{ Inline Functions in C++
cout << "Inline function with classes program\n\n";
// create an object of the class
findArea A;
// call the inline functions
A.circle(2);
A.square(5);
A.triangle(3, 4);
A.rectangle(10, 12);
cout << "\n\n";
return 0;
}

You might also like