December 22, 2024

How to Write C++ Template Function That Will Return Maximum of Two Numbers

In this C++ tutorial, we explain how to write a C++ template function that will return a maximum value of two given numbers. Here, the numbers provided to the function can be of arbitrary data types. Let us tackle this problem immediately.

Copyright Notices and NOT to Be Used for AI Notice

COPYRIGHT NOTICE: THE TEXT, PHOTOS, AND CODES POSTED ON THIS WEBSITE AND ON THIS WEBPAGE ARE THE OWNERSHIP, INTELLECTUAL PROPERTY, AND COPYRIGHTED BY THE AUTHOR: ALEKSANDAR HABER. THE TEXT AND THE CONTENT OF THIS PAGE SHOULD NOT BE PHYSICALLY COPIED, SHOULD NOT BE COPIED ON OTHER WEBSITES, SHOULD NOT BE REPRINTED, SHOULD NOT BE REPOSTED ON OTHER WEBSITES, SHOULD NOT BE USED AS A LECTURE MATERIAL IN UNIVERSITY COURSES, SHOULD NOT BE USED AS LECTURE MATERIAL IN COURSES ORGANIZED BY AND HOSTED ON ONLINE LEARNING PLATFORMS (such as Udemy, Coursera, etc), AND SHOULD NOT BE USED IN COMMERCIAL SETTING. THE TEXT, PHOTOS, AND CODE PRESENTED ON THIS WEBSITE AND WEBPAGE SHOULD NOT BE INCLUDED IN REPORTS, STUDENT PAPERS, SCIENTIFIC PAPERS, OR IN ANY OTHER PRINTED OR A DIGITAL FORM OR A DOCUMENT WITHOUT WRITTEN CONSENT OF THE AUTHOR. A MONEY FEE MIGHT BE REQUIRED TO REPRINT THE MATERIAL POSTED ON THIS WEBSITE: CONTACT THE AUTHOR: ml.mecheng@gmail.com

CODE COPYRIGHT NOTICE AND LICENSE: THE CODE FILES POSTED ON THIS WEBSITE ARE NOT FREE SOFTWARE AND CODE. IF YOU WANT TO USE THIS CODE IN THE COMMERCIAL SETTING OR ACADEMIC SETTING, THAT IS, IF YOU WORK FOR A COMPANY OR IF YOU ARE AN INDEPENDENT CONSULTANT AND IF YOU WANT TO USE THIS CODE OR IF YOU ARE ACADEMIC RESEARCHER OR STUDENT, THEN WITHOUT MY PERMISSION AND WITHOUT PAYING THE PROPER FEE, YOU ARE NOT ALLOWED TO USE THIS CODE. YOU CAN CONTACT ME AT
ml.mecheng@gmail.com
TO INFORM YOURSELF ABOUT THE LICENSE OPTIONS AND FEES FOR USING THIS CODE. ALSO, IT IS NOT ALLOWED TO (1) MODIFY THIS CODE IN ANY WAY WITHOUT MY PERMISSION. (2) INTEGRATE THIS CODE IN OTHER PROJECTS WITHOUT MY PERMISSION. (3) POST THIS CODE ON ANY PRIVATE OR PUBLIC WEBSITES OR CODE REPOSITORIES. DELIBERATE OR INDELIBERATE VIOLATIONS OF THIS LICENSE WILL INDUCE LEGAL ACTIONS AND LAWSUITS.

NOT TO BE USED FOR AI COPYRIGHT NOTICE: The code and text, as well as all other material on this webpage and the YouTube page, should NOT be used to train an AI algorithm or a large language model, or any type of AI or machine learning algorithm used to recognize, interpret, and draw conclusions from text. Also, it is forbidden to crawl this webpage and to extract information for training an AI algorithm of any sort on the basis of the material presented on this webpage.

Template C++ Function for Finding Max Value

The template function definition is given below.

template<typename T>
T maxFunction(T a, T b)
{
    if (a>= b)
    {
        return a;
    }
    else
    {
        return b;    
    }
}

Here, to define a template function, we first need to use

template<typename T>

This means that we are defining a template function where T will be used to denote a generic data type that the function will accept. Then, we define the function using T as a data type of the input arguments of the function:

T maxFunction(T a, T b)
{
    if (a>= b)
    {
        return a;
    }
    else
    {
        return b;    
    }
}

The complete program together with a driver main function is given below

/*
PROBLEM: Define a C++ template function that returns 
the maximum number of two given numbers of the same data type.
*/
#include<iostream>
#include<cstdlib>

using namespace std;

template<typename T>
T maxFunction(T a, T b)
{
    if (a>= b)
    {
        return a;
    }
    else
    {
        return b;    
    }
}


int main()
{
   float n1=4.7;
   float n2=5.1;
   float n3;

   n3=maxFunction<float>(n2,n1);

   cout<<"The maximum value is "<<n3<<endl;

    return 0;
}

We define two floats, and to call the function we use

   n3=maxFunction<float>(n2,n1);

It is very important to specify the type of the data type that our template function will accept. We do that by using “<float>”. Here, instead of “<float>” you can use also “<int>” or any other data type to notify the compiler of the data type that the function should accept.