Loading

Function Templates in C++

Submitted by: 
Language: 

Function Templates in C++

In this tutorial you will learn:
1. What are templates?
2. Why to use function templates?
3. How to use template functions in a program?
4. Basic C++ syntax

What are templates?
Sometime we have a function in which we need to have different data types of input on which we have to perform some operations, this function would obviously return some value. So in C++ to serve the purpose we can make functions called template functions which allow us to turn the inputs and the outputs of the function to the specific data type as per the requirement of the user. There are two type of templates which are Function templates and Class templates. Here we will be discussing only the function templates.

Why use function templates?

We use function templates because it reduces our code, saves our time and let us avoid errors as it allows us to write only one function which can be used for multiple data types. Function templates make it easier for us to code our program generically instead of specifically, making it more usable and understandable for the programmer.
How to use template functions?
We first need to write some code that will specify the compiler that we will be using template functions in this program. It is done as
template
Here T is just an alphabet we may have any of the alphabet from A-Z as per our comfort and likeness.
Next part is that while defining a function we replace every input and the output data type with T so that our function makes use of the template. Like this
T Min(T a, T b)
{
if (a < b)
return a;
else
return b;
}

Program:
strong>Template Functions

Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;

These are the basic header files which are required for C++ programming. Using namespace std is used at the top of the program because if its not written then we have to write ‘std::’ with every line of the program. #include includes the C++ built in libraries and iostream is used for input and output of the program. Conio.h is used to show output on the screen, if it is not present , the screen flashes and disappears hence not showing output.

  1. template <class T>
  2. T abs (T n)
  3. {
  4. return (n<0) ? -n:n;
  5. }

In this part of code we have made a template function, this function takes in T type input and returns a T type variable. Here T provides generic data type. abs is the function that takes in a T type variable and return a T type variable. T is decided by the user input according to the requirements. Here we have a decision perimeter “?” which checks if the number is positive it returns the number as it is or else if number is negative it returns the negative of that number, as negative multiply by negative is a positive number so it also gets positive hence absolute value of that number is gained.

  1. void main()
  2. { int a;
  3. int b;
  4. double c;
  5. double d;
  6. cout<<"Please Input a positive integer number:"<<endl;
  7. cin>>a;
  8. cout<<"\n Please Input a negative integer number:"<<endl;
  9. cin>>b;
  10. cout<<"\n Please Input a positive non-integer number:"<<endl;
  11. cin>>c;
  12. cout<<"\n Please Input a negative non-integer number:"<<endl;
  13. cin>>d;
  14. cout<<"\n----------------Results---------------------\n";
  15. cout<<"\nAbsolute value of the first input is:"<<abs(a);
  16. cout<<"\nAbsolute value of the second input is:"<<abs(b);
  17. cout<<"\nAbsolute value of the third input is:"<<abs(c);
  18. cout<<"\nAbsolute value of the fourth input is:"<<abs(d)<<endl;
  19. }

In the main of the program we have just input the four numbers, two integer type numbers and two double type numbers. Here it must be seen that while we are displaying the results the compiler when using the abs function changes the T variable according to the input by the user hence this program can be said to be generic data type program.
Execute > compile
then Execute > run
Output


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.