Skip to main content

Simple Function Template Program Example Get Maximum Number in C++

2 min read Updated June 30, 2026
Share:
On this page (8sections)

Generic Programming

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types

Function Template Definition

A function template behaves same as normal function except that the template works with different data types.

Function Template Syntax

template <class identifier> function_declaration;
template <typename identifier> function_declaration;

templete_identifier fn_name(templete_identifier .. args) {
 return ...
}

Example Program

// Header Files
#include<iostream>
#include<stdio.h>
#include<conio.h>

using namespace std;

// Template Declaration

template<class T>

// Template Function
T getMaximun(T x, T y) {
    if (x > y)
        return x;
    else
        return y;
}

int main() {
    int a, b, i;
    float c, d, j;

    cout << "Function Template Programs : Get Maximum Number \n";
    cout << "Enter A,B values(integer):";
    cin >> a>>b;
    i = getMaximun<int>(a, b);
    cout << "Result Max Int : " << i;

    cout << "\n\nEnter C,D values(float):";
    cin >> c>>d;
    j = getMaximun<float>(c, d);
    cout << "Result Max Float : " << j;

    getch();
    return 0;
}

Sample Output

Function Template Programs : Get Maximum Number
Enter A,B values(integer):56
89
Result Max Int : 89

Enter C,D values(float):17.99
9.01
Result Max Float : 17.99

How It Works

This C++ program demonstrates Simple Function Template Program Example Get Maximum Number. It first reads the required values as input, then uses conditional logic to decide the result, and finally prints the output shown in the Sample Output above.

  1. Declare the variables that hold the program’s data.
  2. Read the input values that the program will work with.
  3. Use conditional statements to handle the different cases.
  4. Print the final result to the console so you can compare it with the sample output.

Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.

Continue learning with these related tutorials and programs:

Frequently Asked Questions

What does this C++ program do?
It is a C++ example program that demonstrates Simple Function Template Program Example Get Maximum Number, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses conditional logic, reading user input and user-defined functions, illustrating a common pattern in C++ programming.

Related Tutorials

Search tutorials