Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3."— Presentation transcript:

1 1

2 2

3 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3

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

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

6 FUNCTION DECLARATION FUNCTION CALL FUNCTION DEFINITION 6

7 7

8 1.Call by value 2.Call by reference 8

9 9

10 10

11 11

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

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

14 14

15 15

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

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

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

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

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

21 #include 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; } 21

22 #include 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; } 22

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 23

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

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

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

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 27

28 28


Download ppt "1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3."

Similar presentations


Ads by Google