Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Lecture 5

CSE202: OBJECT ORIENTED PROGRAMMING


INLINE FUNCTIONS
INTRODUCTION

• Normally, when a function is called , control is transferred


from calling environment to the function definition by a branch
or call instruction which causes time overhead.
• With inlining, the programmer has requested the compiler to
insert the complete body of the function in every place the
function was called rather than jumping to function definition
each time.
• This optimization may improve time and space usage at run
time, at the possible cost of increasing the final size of the
program.
Inline Functions

• Each time you call a function in a C++ program, the


computer must do the following:
– Remember where to return when the function eventually ends

– Provide memory for the function’s variables

– Provide memory for any value returned by the function

– Pass control to the function

– Pass control back to the calling program

• This extra activity constitutes the overhead, or cost of


doing business, involved in calling a function.
• Solution to this is to replace each function call with the
necessary code instead of making function.
• Inline function is a function which gets expanded inline at
each point in the program where it is called.
• In other words inline functions are those functions whose
function body is inserted in place of the function call during
the compilation process.
• Syntax:
inline ret_type func_name(parameter_list)
{
function body
}
inline int max(int x,int y)
{
return (x>y?x:y);
}
int main()
{
int m=10,n=25;
int a,b;
a=max(6,8);
cout<<“greatest is”<<a;
b=max(m,n);
cout<<“greatest of m=“<<m<<“ and n=“<<n <<“is “<<b;
getch();
return 0;
}
• Output :
Greatest of 6 and 8=8
Greatest of m=10 and n=25 is 25
• An inline function is a small function with no
calling overhead
• Overhead is avoided because program control never
transfers to the function
• A copy of the function statements is placed directly
into the compiled calling program
• The inline function appears prior to the main(),
which calls it
• Any inline function must precede any function that
calls it, which eliminates the need for prototyping in
the calling function
• When you compile a program, the code for the
inline function is placed directly within the
main() function

• You should use an inline function only in the


following situations:
– When you want to group statements together so that you can use a
function name

– When the number of statements is small (one or two lines in the


body of the function)

– When the function is called on few occasions


Disadvantages:

• All functions that uses the inline function must be


recompiled if any changes are made to the inline
function.
• Although inline function reduces the execution time.
It increases the size of the executable file as multiple
copies of the inline functions body are inserted in
the programs. So it is always recommended to use
inline functions for small frequently used functions.
• The definition of inline function should appear
before the function call.
C++ inline function
• C++ inline function is powerful concept that is
commonly used with classes. If a function is inline,
the compiler places a copy of the code of that
function at each point where the function is called at
compile time.
• Any change to an inline function could require all
clients of the function to be recompiled because
compiler would need to replace all the code once
again otherwise it will continue with old
functionality.
#include <iostream>
using namespace std;
inline int Max(int x, int y) {
return (x > y)? x : y;
}
int main() {
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}
Inline Member Function
• To inline a function, place the keyword inline before the
function name and define the function before any calls are
made to the function. The compiler can ignore the inline
qualifier in case defined function is more than 3-5 lines.
• A function definition in a class definition is an inline function
definition, even without the use of the inline specifier.
A member function that is defined inside its class member list is
called an inline member function.
An equivalent way to declare an inline member function is to
either declare it in the class with the inline keyword (and define
the function outside of its class) or to define it outside of the class
declaration using the inline keyword.
#include <iostream>
using namespace std;
class operation
{ Method 1: Inline Member Function
int a,b,add;
public:
void get() // inline Member Function
{ int main()
cout << "Enter first value:"; {
cin >> a; cout << "Program using inline function\n";
cout << "Enter second value:"; s.get();
cin >> b; s.sum();
} return 0;
void sum(); }
}s;
inline void operation :: sum() Method 2: Inline Member Function
{
add = a+b;
cout <<"Addition of two numbers: " << a+b;
}
Note: Inlining is only a request to the compiler, not a command.
Compiler can ignore the request for inlining. Compiler may not
perform inlining in such circumstances like:

1) If a function contains a loop. (for, while, 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 function body.
5) If a function contains switch or goto statement.
Inline functions provide following advantages:

1) Function call overhead doesn’t occur.

2) It also saves overhead of a return call from a function.

3) When you inline a function, you may enable compiler to perform


context specific optimization on the body of function. Such
optimizations are not possible for normal function calls. Other
optimizations can be obtained by considering the flows of calling
context and the called context.
Inline function disadvantages:
1. If you use too many inline functions then the size of the binary
executable file will be large, because of the duplication of same code.

2. 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.

3. 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 compiler would require to
replace all the code once again to reflect the changes, otherwise it
will continue with old functionality.
When the function is defined inside a class, it is treated as
………………….

A) data function
B) inline function
C) non inline function
D) member variable
When the function is defined inside a class, it is treated as
………………….

A) data function
B) inline function
C) non inline function
D) member variable
Function which are declared in a class declaration and defined
outside the class is known as……………….

A) inline member function


B) non-inline member function
C) static member function
D) dynamic member function
Function which are declared in a class declaration and defined
outside the class is known as……………….

A) inline member function


B) non-inline member function
C) static member function
D) dynamic member function

You might also like