Tech

Function Overloading–Functions–STRUCTURED PROGRAMMING Course Notes

Function Overloading

  • C++ enables several functions fo the same name to be defined, as long as these functions have different sets of parameters (a.k.a.–different signatures). This capability is called function overloading.
  • When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types, and order of the arguments in the call.
  • Overloaded functions are distinguished by their signatures (function name & parameter types).
  • The compiler encodes each function identifier with the number & types of its parameters to enable type-safe linkage. Type-safe linage ensures that the proper overloaded function is called and that the types of the arguments conform to the types of the parameters.
  • Function overloading is used to create several functions of the same name that perform similar tasks, but on different data types.
    • For example, many functions in the math library are overloaded for different numeric types–the C++ standard requires ‘float’, ‘double’, and ‘long double’ overloaded versions of the math library functions.
    • Note: Overloading functions that perform closely related tasks can make programs more readable & understandable.