Cpp Tutorial: Function Template in C++

In this tutorial we shall learn about below topics
Introduction to Function template
Syntax for function template:
Example for simple function template
Function Template with more arguments
Overloading function template
Recursion with function template

1. Introduction to Function template

The function defined outside a class is called as normal functions.
Generally normal function will work with only 1 data type.
But function template will work with any kind of data types.

2. Syntax for function template:

1. “template” is the keyword used to declare a template.
2. Followed by list of arguments.
Syntax:
template < class T>
void myFunc( T num)
{

}
So from the line “template <class T>”. “template” is the keyword used. “class T”, is the argument list. If you want to have more arguments, then you need to give “class T, class S”.
It is not mandatory to give capital letter for the argument name, but it is a common practice.

3. Example for simple function template

In the below program we shall see a simple function accepting one argument and checking if the value is greater than 10 or not.
We test this function by calling the function with different data type values.
#include <iostream>
using namespace std;

template <class T>
void compare(T a)
{
    if (a > 10)
    {
        cout<<a<<" is greater than 10"<<endl;
    }
    else
    {
        cout<<a<<" is lesser than 10"<<endl;
    }
    
    
}

int main(void)
{
    compare(4);
    compare(10.34);
    compare(-6);
    
}
Output:
4 is lesser than 10
10.34 is greater than 10
-6 is lesser than 10

4. Function Template with more arguments

It is also possible to create a function with more arguments. It can be done as shown below;
#include <iostream>
using namespace std;

template <class T, class S>
void compare(T a, S b)
{
    if (a > b)
    {
        cout<<a<<" is greater than "<<b<<endl;
    }
    else
    {
        cout<<a<<" is lesser than "<<b<<endl;
    }
    
    
}

int main(void)
{
    compare(4, 5);
    compare(10.34, 6);
    compare(-6, 10.34);
    
}
Output:
4 is lesser than 5
10.34 is greater than 6
-6 is lesser than 10.34

5. Overloading function template

We can also overload the function template.
Implicit conversion will not be carried out during overloading of template function.
If no accurate match is found, error will be returned.
Normal function match will be given higher precedence than template function match.
Example:
#include <iostream>
using namespace std;

template <class T>
void display(T a)
{
    cout<<"Template function is called for "<<a<<endl;
}

void display(int a)
{
    cout<<"Integer function is called for "<<a<<endl;
}

int main(void)
{
    display(4);
    display(10.34);
    display('A');
    
}
Output:
Integer function is called for 4
Template function is called for 10.34
Template function is called for A

6. Recursion with function template

Template function will also supports recursion.
In below example we shall see how to get factorial of a number using template and recursion.
#include <iostream>
using namespace std;

template <class T>

long int factorial(T n) 
{
    if (n>=1)
        return n*factorial(n-1);
    else
        return 1;
}

int main(void)
{
    int num = 5;
    cout<<"The factorial of "<<num <<" using template recursion is "<<factorial(num)<<endl;
    
}

Output:

The factorial of 5 using template recursion is 120
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *