28
FUNCTION AND INLINE FUNCTION 1

1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

Embed Size (px)

Citation preview

Page 1: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

1

FUNCTION AND INLINE

FUNCTION

Page 2: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

2

CREATED BY:

1.MADHURI R. NAGAPURKAR

2.SHRUTI S. KULKARNI3.PRERANA A. NALANGE

Page 3: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

3

POINTS

FUNCTIONINLINE FUNCTIONDIFFERENCE BETWEEN FUNCTION AND

INLINE FUNCTIONCONCLUSION

Page 4: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

4

FUNCTION

DEFINITION:

A function is a group of statement that together perform a task and reduce a complexity of a program.

Page 5: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

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.

Page 6: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

6

How to declare a function?

• FUNCTION DECLARATION•FUNCTION CALL•FUNCTION DEFINITION

Page 7: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

7

SYNTAX

void add( ); //function declarationmain( ){----------add( ); //function call----------}void add( ) // function definition{-----------------}

Page 8: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

8

TWO TYPES OF FUNCTION CALLS ARE AS FOLLOW

1.Call by value2.Call by reference

Page 9: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

9

CALL BY VALUE 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}

Page 10: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

10

CALL BY REFERENCE 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 }

Page 11: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

11

NEED OF FUNCTION IN PROGRAM

1.If a block of code is repeated many times then a function to execute that would save a greatdeal of space and make a program more

readable.

2.The function breaks a complex program into logical parts so as to understand quickly.

Page 12: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

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.

Page 13: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

13

what is macro?

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

Page 14: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

14

Inline Function

•DEFINITION: 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.

Page 15: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

15

SYNTAX

prototype fun_name( ); //function declarationmain( ){--------------------------fun_name( ); //function call}inline prototype fun_name( ); //function definition {-------}

Page 16: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

16

WHY THERE IS A NEED OF INLINE FUNCTIONS?

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

Page 17: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

17

SOME IMPORTANT POINTS

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.

Page 18: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

18

When to use ?

•Function can be made as inline as per programmer need. Some useful recommendation are mentioned below-

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

Page 19: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

19

Consider the following example

int sum(int a, int b) { return a + b; } void print_sum() { int r = sum(5,6); printf("%d\n", r); } 

Page 20: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

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

Page 21: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

21

Program #include <iostream>

using namespace std;

inline int sqr (int x) //defined function as inlined{           int y;           y = x * x;           return y;}int main(){               int a =3, b; // declaration of variables               b = sqr(a); //function call               cout <<b;               return 0;}

Page 22: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

22

Program#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; }

Page 23: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

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

Page 24: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

24

Function Vs Inline Function

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

Page 25: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

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.

Page 26: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

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.

Page 27: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

27

SOME OF THE SITUATION WHERE INLINE EXPANSION MAY NOT WORK

• 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

Page 28: 1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

28

THANK YOU !!!