C++ Function Templates
C++ Function Templates
Function templates are another application of templates. Class templates were used to program generic classes. Similarly, function templates are used to define generic functions to be able to use a function independent of its data types for different data type combinations.
Example of some of the in-built function templates are,
sort(), max(), min() etc.
The syntax for constructing a function template is,
An example of a somewhat data type independent function might be a function to find the average of two numerical values. Now, this function might expect any of the data type combinations between an integer, a float, or a double value. So, here is how we build a function so that it is generalized for finding the average of two numerical values.
Output
Both class templates and function templates help reduce the size of the program and make it more readable.