Pages

Thursday, October 19, 2017

Easy tutorial for C / C ++ - inline function

Easy tutorial for C / C ++ - inline function

inline function :

- There is an inline function that plays a similar role to the macro function and compensates the disadvantage of the macro function.

- In the case of a macro function, it was substituted within a program instead of calling a function unlike a normal function. For some cases, we could get wrong results depending on the presence of parentheses, as we saw in the previous example.

- Inline functions have the advantage of speeding up execution and are usually used for functions with short length. The structure of an inline function is the same as that of a normal function definition, and only "inline" is added before the data type.

'(0) inline' '(1) data type' '(2) function name' ('(3) arguments')
{
  '(4) Execution sentences';
  '(5) return (' result value ');
}

'(0) inline': Indicates that this is an inline function.

'(1) Data type': Data type of return value.
'(2) Function name': This is the part that creates the function name.

'(3) Arguments': Arguments to be used in the function. Of course, some functions do not have arguments.

'(4) Execution sentences': A collection of execution statements that perform specific functions.
'(5) return (' result value '): The return value of the function may not be returned.
- Let's look at a simple example.

Example Code

#include <iostream>
#define tMac 3
#define sumM1(x) x + x

using namespace std;

int C_func(int x){
   int r;
   r = x + x ;
   return r;
}

inline int I_func(int x){
   int r;
   r = x + x ;
   return r;
}

int main() {

          cout << "Macro function" << endl ;
          cout << " 2* (3 + 3) = " << 2 * sumM1(tMac) << endl ;
          cout << endl;

          cout << "Function" << endl ;
          cout << " 2* (3 + 3) = " << 2 * C_func(tMac) << endl ;
          cout << endl;

          cout << "Inline function" << endl ;
          cout << " 2* (3 + 3) = " << 2 * I_func(tMac) << endl ;
          return 0;
}

- We will use the previous example to compare the results of macro, function, and inline function. The macro constant 'tMac' to be used in the operation has been replaced by 3.

(1) sumM1(x) x + x: Macro function that takes the argument 'x'.

(2) C_func: This is a function that takes the integer argument 'x' and performs the operation of 'x + x' and returns the result.

(3) I_func: This is an inline function that takes the integer argument 'x' and performs the operation of 'x + x' and returns the result.

- Let's print the result of simple operation 2 * (3 + 3).

results :

Macro function
 2* (3 + 3) = 9

Function
 2* (3 + 3) = 12

Inline function
 2* (3 + 3) = 12

(1) In the case of the macro function, the result "2 * (3 + 3) = 8" is obtained as in the previous example. Since the macro function is to substitute instead of returning the result, if it expresses the above expression, it becomes 2 * 3 + 3 and gives result of 8.

(2), (3) functions and inline functions can get the right results.

No comments:

Post a Comment