Like class templates, we can also define function templates that could be used to create a family of functions with different argument types. The general format of a function template is as follows:

The function template syntax is similar to that of the class template except that we are defining functions instead of classes. We must use the template parameter T as and when necessary in the function body and in its argument list.

The following example declares a swap() function template that will swap two values of a given type of data.

This essentially declares a set of overloaded functions, one for each type of data. We can invoke the swap() function like any ordinary function.

Example:

Output:

Function Templates with Multiple Parameters

We can use more than one generic data type in the template statement, using a comma- separated list as shown below:

Example: