How to Create a Function Template in C++? Last Updated : 28 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, templates enable us to write generic programs that handle any data type. We can create a template class, function, and variable. A template function is a function that can work with any data type. In this article, we will learn how to create a function template in C++. Create a Function Template in C++To create a template function in C++, we use the template keyword followed by the typename keyword (or class keyword) and a placeholder for the type. Syntax of Function Templatetemplate<typename T> returnType functionName(T parameter1, T parameter2, ...) { // Body of the function } where: template: This keyword can be used to declare the template<typename T>: It can be used to declare the template parameter T.returnType: This can be used to specify the return type.T parameter1, T parameter: These are function parameters of type T.C++ Program to Create a Function Template C++ // C++ Program to illustrate how to Create a Template // Function #include <iostream> using namespace std; template <typename T> T max_value(T a, T b) { return (a > b) ? a : b; } // Driver Code int main() { // Example usage of the max template function with // different data types cout << "Max of 5 and 10: " << max_value(5, 10) << endl; cout << "Max of 3.5 and 7.2: " << max_value(3.5, 7.2) << endl; return 0; } OutputMax of 5 and 10: 10 Max of 3.5 and 7.2: 7.2 Comment More infoAdvertise with us Next Article How to Create a Function Template in C++? S syam1270 Follow Improve Article Tags : C++ Programs C++ cpp-template CPP Examples Practice Tags : CPP Similar Reads How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read How to Create a Pure Virtual Function in C++? In C++, pure virtual functions are those functions that are not implemented in the base class. They are instead implemented in the derived classes if necessary. In this article, we will discuss how to create a pure virtual function in a class in C++. How to Create a Pure Virtual Function in C++? To 2 min read How to Create a Template Class in C++? In C++, template classes are used to create generic classes that can work for different data types. In this article, we will learn how to create a template class in C++. Create a Template Class in C++ To create a template class in C++, we can follow the below syntax: Syntax of Template Classtemplate 2 min read How to Create a Thread in C++? A thread is a basic element of multithreading which represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to create a thread in C++. How to Create a Thread in C++?In C++, the std::thread is a class template that is used t 2 min read How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi 2 min read Like