1.

What do you understand about inline functions?

Answer»

In C or C++, an inline function is a function that is declared using the keyword "inline." It has two FUNCTIONS:

  • It is a compiler DIRECTIVE that requests that the compiler replace the body of the function inline by performing inline expansion, that is, placing the function code at the location of each function call, thus reducing the overhead of a function call (it is not mandatory for the compiler to comply with the request of the inline function). In this WAY, it is analogous to the register storage class specifier, which also provides an optimization indication. For frequent calls to the same function, inline functions are typically employed.
  • The keyword "inline" has a second goal: to change the behaviour of links. The C/C++ separate compilation and linkage architecture requires this, in part because the function's definition (body) must be reproduced in all translation units where it is used to allow inlining during compilation, which causes a collision during linking if the function has external linkage (it violates uniqueness of external symbols). C and C++ (as well as dialects such as GNU C and Visual C++) handle this in different ways.

Given below is an example to understand inline functions in a better way:

inline void multiplyNums(int x, int y){ int answer = x * y; printf("Product of the two numbers given is: %d", answer);}

Let us assume that the above inline function is called at someplace in the main function of our C program as SHOWN below:

int i = 20;int j = 40;multiplyNums(i, j);

Note that the "multiplyNums(i, j);" function call will be replaced by the following piece of code in the main function itself by the C compiler:

int i = 20;int j = 40;int answer = i * j;printf("Product of the two numbers given is: %d", answer);


Discussion

No Comment Found