• Now Online : 29
  • admin@codemyne.net


In any programming language, the best way to increase the modularity of a large program is to use functions. The proper usage of functions to perform independent tasks greatly increases the readability of the program and reduces maintenance costs. However, there is one hitch to this concept.

A call to function causes the program to be suspended and control to be passed to the function being called. The program branches to the called function, and when the execution is complete, control is returned to the program and execution continues from the line following the function. Thus, a function call involves an overhead in terms of the time taken to execute these additional steps.

Sometimes, a member function may be very small (say, just a single line of code, like the getword function in the word class). In such cases, the time taken to make the function call is far more than that taken to execute the function code itself. Hence, the program can be made faster by replacing the function call with the actual body of the function itself. This can be made possible in C++ by declaring the function as inline. A function can be made inline in two ways:

  • By writing the code of the function itself within the class declaration, i.e.., without using the scope operator.
  • By using the INLINE keyword before the function definition. For example,
inline char *word: :getword()
    return this->str_word;

The inline specifier is only a suggestion to the compiler. All functions declared as inline need not be treated as inline by the computer. If the function happens to recursive or if the function body is too large, the compiler just ignores the inline specification. Because it will not be a good idea to substitute 300 lines of function code in place of a simple function call.

And in the case of recursive functions, the function body can’t be substituted each time the function is called because the number of recursions is not known at the time of compilation.

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment