C++ Inline Function

  Home  C++ Programming  C++ Inline Function


“C++ inline function job preparation guide for freshers and experienced candidates. Number of C++ Inline Function frequently asked questions(FAQs) asked in many interviews”



27 C++ Inline Function Questions And Answers

1⟩ What is an inline function?

An inline function is a combination of macro & function. At the time of declaration or definition, function name is preceded by word inline.

When inline functions are used, the overhead of function call is eliminated. Instead, the executable statements of the function are copied at the place of each function call. This is done by the compiler.

Consider following example:

#include <iostream>

using namespace std;

inline int sqr(int x)

{

int y;

y = x * x;

return y;

}

int main()

{

int a =3, b;

b = sqr(a);

cout <<b;

return 0;

}

Here, the statement b = sqr(a) is a function call to sqr(). But since we have declared it as inline, the compiler replaces the statement with the executable stmt of the function (b = a *a)

Please note that, inline is a request to the compiler. If it is very complicated function, compiler may not be able to convert it to inline. Then it will remain as it is. E.g. Recursive function, function containing static variable, function containing return statement or loop or goto or switch statements are not made inline even if we declare them so.

Also, small functions which are defined inside a class (ie their code is written inside the class) are taken as inline by the compiler even if we don’t explicitly declare them so. They are called auto inline functions.

 202 views

2⟩ Explain what are static member functions?

Static member functions are used to maintain a single copy of a class member function across various objects of the class. Static member functions can be called either by itself, independent of any object, by using class name and :: (scope resolution operator) or in connection with an object.

Restrictions on static member functions are:

1. They can directly refer to other static members of the class.

2. Static member functions do not have this pointer.

3. Static member function can not be virtual.

 212 views

3⟩ Can you please explain the difference between inline functions and macros?

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. There are two kinds of macros: Object-like macros and function-like macros.

Inline function is a function that is expanded in line when the function is called. That is the compiler replaces the function call with the function code (similar to macros).

The disadvantage of using macros is that the usual error checking does not occur during compilation..

 200 views

4⟩ Tell me what happens when recursion functions are declared inline?

The call to the body of the function is replaced by an inline function. This reduces the saving context on stack overhead. This process is efficient when the size of the function is small and invoked occasionally. Deep nesting of a method is done when a function is invoked recursively. The inline function is invoked recursively, and every call to itself is replaced with the body of the function, thus consumes a lot of code space.

 212 views

5⟩ Please explain, do inline functions improve performance?

A function when defined as INLINE, the code from the function definition is directly copied into the code of the calling function.

It avoids the overhead of calling the actual function. This is because the complier performs and inline expansion which eliminates the time overhead when a function is called.

Reduces space as no separate set of instructions in memory is written.

 204 views

6⟩ Explain advantages and disadvantages of using macro and inline functions?

A textual substitution is provided by a macro as a constant, where as an inline function is procedure which is called at each time. Although the macros have few advantages over inline functions, the disadvantages are numerous. For example, a macro can not perform type checking and validation, as these operations are performed in a function at the most.

Everyone should decide for themselves to use them, but the use of inline functions over macros is advocated by Bjarne Struoustrup, the creator of C++. The imperative features of inline functions are frequently used with classes in C++. There is similarity between invoking normal functions and inline functions, except that, inline functions are never actually called. The inline functions, as their name suggests, are expanded in line at every time of invocation. All that is needed to invoke an inline function is to prefix the key word ‘inline’ to the function.

 206 views

8⟩ described when recursion functions are declared inline?

The call to the body of the function is replaced by an inline function. This reduces the saving context on stack overhead. This process is efficient when the size of the function is small and invoked occasionally. Deep nesting of a method is done when a function is invoked recursively. The inline function is invoked recursively, and every call to itself is replaced with the body of the function, thus consumes a lot of code space.

 201 views

10⟩ Default values for function are need to be specified from left to right only. a. True b. False

b. False

Explanation:

Default values need to be specified from Right to Left order.

Example:

void calculate(int amt, int years, float rate=7.8); //valid

void calculate(int amt, int years=5, float rate=7.8); //valid

void calculate(int amt=21000, int years, float rate=7.8); //Invalid

Third statement is invalid as we skipped second parameter of the function. Rule says that default values should be set from Right to Left order only. We cannot provide a default value to specific parameter in the middle of an parameter list.

 226 views

12⟩ Described the disadvantages of using macro and inline functions?

Everyone should decide for themselves to use them, but the use of inline functions over macros is advocated by Bjarne Struoustrup, the creator of C++. The imperative features of inline functions are frequently used with classes in C++. There is similarity between invoking normal functions and inline functions, except that, inline functions are never actually called. The inline functions, as their name suggests, are expanded in line at every time of invocation. All that is needed to invoke an inline function is to prefix the key word 'inline' to the function.

 199 views

13⟩ Does the inline functions improve performance?

A function when defined as INLINE, the code from the function definition is directly copied into the code of the calling function.

★ It avoids the overhead of calling the actual function. This is because the complier performs and inline expansion which eliminates the time overhead when a function is called.

★ Reduces space as no separate set of instructions in memory is written.

 198 views

14⟩ Explain the difference between inline functions and macros?

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. There are two kinds of macros: Object-like macros and function-like macros.

Inline function is a function that is expanded in line when the function is called. That is the compiler replaces the function call with the function code (similar to macros).

The disadvantage of using macros is that the usual error checking does not occur during compilation.

 189 views

15⟩ Described the advantages of using macro and inline functions?

A textual substitution is provided by a macro as a constant, where as an inline function is procedure which is called at each time. Although the macros have few advantages over inline functions, the disadvantages are numerous. For example, a macro can not perform type checking and validation, as these operations are performed in a function at the most.

 202 views

16⟩ Explain static member functions?

A static function can have an access to only other static members (functions or variables) declared in the same class.

A static member function can be called using the class name instead of its objects.

E.g. classname :: functionname;

 221 views

17⟩ Explain inline function?

An inline function is a combination of macro & function. At the time of declaration or definition, function name is preceded by word inline.

When inline functions are used, the overhead of function call is eliminated. Instead, the executable statements of the function are copied at the place of each function call. This is done by the compiler.

Consider following example:

#include <iostream>

using namespace std;

inline int sqr(int x)

{

int y;

y = x * x;

return y;

}

int main()

{

int a =3, b;

b = sqr(a);

cout <<b;

return 0;

}

 245 views

18⟩ Define Inline Function?

When the function is defined Inline, the C++ compiler puts the function body inside the calling function. You can define function as Inline when the function body is small and need to be called many times, thus reduces the overhead in calling a function like passing values, passing control, returning values, returning control.

 192 views