Inline Function

1
2
3
FUNCTION
INLINE FUNCTION
DIFFERENCE BETWEEN FUNCTION AND
INLINE FUNCTION
CONCLUSION
4
DEFINITION:
A function is a group of statement that
together perform a task and reduce a
complexity of a program.
5
• Functions are building blocks of C and C++ and
the place where all activity occurs.
• The general form of functions is
ret-type function-name(parameter list)
{
body of the function
}
while declaring parameters data type should be
same.
6
• FUNCTION DECLARATION
• FUNCTION CALL
• FUNCTION DEFINITION
7
void add( );
main( )
{
---------add( );
---------}
void add( )
{
----------------}
//function declaration
//function call
// function definition
8
1.Call by value
2.Call by reference
9
In this arguments are passed
‘by value’ this means that called function is given
values of the arguments in temporary variables
rather than originals. In this case, the changes
made to the parameter have no effect on the
argument.
Ex. void add(int a , int b); // function declaration
main( )
{
add( x, y); //function call by value
}
10
In this method the address of an
argument is copied to the parameter. Inside the
subroutine , the address is used to access the
actual parameter in the call. Here the changes to
parameter affect argument.
Ex. void swap(int *x,int *y);//function declaration
main ( )
{
swap ( &a , &b ); // function call by reference
}
11
1.If a block of code is repeated many times then
a function to execute that would save a great
deal of space and make a program more readable.
2.The function breaks a complex program into
logical parts so as to understand quickly.
12
In C we use functions that form a block of
program and makes the program easier to
understand. In C++ an important feature is
supported called as inline functions, that are
commonly used with classes. They make the
function to be expanded in line.
13
• A macro is a fragment of code which has been
given a name. Whenever the name is used, it is
replaced by the contents of the macro.
• We can define macro using #define.
14
•
In C++,the functions that are not
actually called, rather their code is expanded in
line at the point of each invocation such
functions are called as inline functions.
15
prototype fun_name( ); //function declaration
main( )
{
-------------------------fun_name( ); //function call
}
inline prototype fun_name( );
//function definition
{
------}
16
• A significant amount of overhead is generated
by calling and return mechanism of function.
• While calling the function arguments are pushed
onto the stack and saved on various registers
and restore when function returns , this will take
more time to run.
• If we expand a function code inline then
function call produce faster run times.
17
1.Inline function process is similar to using a
macro.
2.Inline is actually just a request, not a command.
3.By marking it as inline, you can put a function
definition in a header file.
18
• Function can be made as inline as per
programmer need. Some useful
recommendation are mentioned below1. Use inline function when performance is
needed.
2. Use inline function over macros.
3. Prefer to use inline keyword outside the class
with the function definition to hide
implementation details.
19
int sum(int a, int b)
{
return a + b;
}
void print_sum()
{
int r = sum(5,6);
printf("%d\n", r);
}
20
If you declare your function as inline:
inline int sum(int a, int b)
{
return a + b;
}
• The compiler will replace the actual function call
to the actual body of your function, thus, the
resulting binary will have something like:
void print sum()
{
int r = 5 + 6;
printf("%d\n", r);
}
21
#include <iostream>
using namespace std;
inline int sqr (int x)
{
int y;
y = x * x;
return y;
}
int main()
{
int a =3, b;
b = sqr(a);
cout <<b;
return 0;
}
//defined function as inlined
// declaration of variables
//function call
22
#include <iostream.h>
using namespace std;
inline int Max(int x, int y) //defined function as inlined
{
return (x > y)? x : y;
}
int main( )
// Main function for the program
{
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;
}
23
• When the above code is compiled and executed, it
produces the following result :
Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010
24
• In many places we create the functions for small
work/functionality which contain simple and
less number of executable instruction.
• Imagine their calling overhead each time they
are being called by callers.
25
• When a normal function call instruction is
encountered, the program stores the memory
address of the instructions immediately following
the function call statement, loads the function
being called into the memory, copies argument
values, jumps to the memory location of the called
function, executes the function codes, stores the
return value of the function, and then jumps back
to the address of the instruction that was saved just
before executing the called function.
• Too much run time overhead.
26
• The C++ inline function provides an alternative.
With inline keyword, the compiler replaces the
function call statement with the function code itself
(process called expansion) and then compiles the
entire code.
• Thus, with inline functions, the compiler does not
have to jump to another location to execute the
function, and then jump back as the code of the
called function is already available to the calling
program.
27
• If the function code is large.
• If the function is recursive function .
• If the function contains static variables.
• For function returning values, if a loop, a
switch , or a goto exists
28