Tech

Function Templates–Functions–STRUCTURED PROGRAMMING Course Notes

Function Templates

  • Overloaded functions typically perform similar operations that involve different program logic on different data types. If the program logic & operations are identical for each data type, overloading may be performed more compactly & conveniently using function templates.
  • Given the argument types provided in calls to a function template, C++ automatically generates separate function template specializations to handle each type of call appropriately.
  • All function template definitions begin with the ‘template‘ keyword followed by a template parameter list to the function template enclosed in angle brackets ( <> ).
  • The formal type parameters are placeholders for fundamental types or user-defined types. These placeholders are used to specify the types of the function’s parameters, to specify the function’s return type and to declare variables within the body of the functions definition.