1 2 3 POINTS FUNCTION INLINE FUNCTION DIFFERENCE
- Slides: 28
1
2
3 POINTS üFUNCTION üINLINE FUNCTION üDIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION üCONCLUSION
4 FUNCTION 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 How to declare a function? • FUNCTION DECLARATION • FUNCTION CALL • FUNCTION DEFINITION
7 SYNTAX void add( ); //function declaration main( ) { -----add( ); //function call -----} void add( ) // function definition { --------}
8 TWO TYPES OF FUNCTION CALLS ARE AS FOLLOW 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 NEED OF FUNCTION IN PROGRAM 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 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.
14 Inline Function • 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 SYNTAX prototype fun_name( ); //function declaration main( ) { -------------fun_name( ); //function call } inline prototype fun_name( ); //function definition { ------}
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.
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.
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.
19 Consider the following example int sum(int a, int b) { return a + b; } void print_sum() { int r = sum(5, 6); printf("%dn", 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("%dn", r); }
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; }
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; }
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 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.
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 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
28 THANK YOU !!!
- Brand frame of reference
- Points of parity and points of difference
- Inline function in c++
- Inline function matlab
- Inline function matlab
- Advantage of inline function
- What is inline function
- Inline function in c++
- What is inline function
- What is inline function
- Inline structure
- Inline internal and external javascript
- Inline definition
- Inline hockey vs ice hockey
- Inline pulsation dampener
- Double bastion inline
- Double bastion inline
- Single bastion inline
- Bioprocessing raman
- Numpy.polynomial.polynomial
- Cython inline
- Dreamweaver inline css
- Cascade inline unit
- Advanced subqueries in sql
- Ich spiele inline-hockey
- Inline hockey vs ice hockey
- Max int c++
- Inline coating
- Pdip vs dip