Tech

Inline Functions, References, & Reference Parameters–Functions–STRUCTURED PROGRAMMING Course Notes

In C++, an empty parameter list is specified by writing either ‘void’ or nothing in parenthesis. Ex: (void) -or- ()

Inline Functions

Inline Functions–C++ provides inline functions to help reduce function call overhead–especially for small functions. Placing the qualifier ‘inline‘ before a function’s return type in the function definition “advises” the compiler to generate a copy of the function’s code in place to avoid a function call.

Note: Any change to an ‘inline’ function requires all clients of the function to be recompiled. This can be significant in some program development & maintenance situations.

Tip: The ‘inline’ qualifier should be used only with small, frequently used functions. Using ‘inline’ functions can reduce execution time but may increase program size.

References & Reference Parameters

  • When an argument is passed-by-value, a copy of the argument’s value is made and passed to the called function. Changes to the copy do NOT affect the original variable’s value in the caller.
  • With pass-by-reference, the caller gives the called function the ability to access the caller’s data directly and to modify it if the called function chooses to do so.
  • A reference parameter is an alias for its corresponding argument in a function call.
  • To indicate that a function parameter is passed by reference, follow the parameter’s type in the function prototype and header by an ampersand(‘&’).
  • All operations performed on a reference are actually performed o the original variable.
  • Ex: ( int &count )
    • When read from right to left is pronounced “count is a reference to an int.”.
  • Tip: One disadvantage of pass-by-value is that, if a large data item is being passed, copying that data can take a considerable amount of execution time & memory space.
  • Tip: Pass-by-reference is good for performance reasons, because it can eliminate the pass-by-value overhead of copying large amounts of data.
  • Tip: Pass-by-reference can weaken security; the called function can corrupt the caller’s data.