Skip to content
SW Study Warehouse
Home › Blog
Blog

Function Template in C++ Programming (Complete 2026 Program & Output)

By Pardeep Patel · November 25, 2026 · 2 min read πŸ‘ 251 Views
print Hello World if Statement C++
Disclosure: Some links in this guide are affiliate links. We may earn a commission at no extra cost to you.

Understanding function template in c++ is an essential building block for beginners and students writing clean, reliable code. Below you will find the step-by-step syntax, working example, and key rules to follow.

Complete Guide: Function Template In C++

To swap the numbers using the concept of function template.
Algorithm
Step 1: Start the program. Step 2: Declare the template class. Step 3: Declare and define the functions to swap the values. Step 4: Declare and define the functions to get the values. Step 5: Read the values and call the corresponding functions. Step 6: Display the results. Step 7: Stop the program.
Program
/* Program to swap the number in C++Β using the concept of function template
Pardeep Patel @ studywarehouse.com */

#include<iostream.h>
#include<conio.h>

template<class t>

void swap(t &x,t &y)
{
t temp=x;
x=y;
y=temp;
}

void fun(int a,int b,float c,float d)
{
cout<<"\na and b before swaping :"<<a<<"\t"<<b;
swap(a,b);
cout<<"\na and b after swaping :"<<a<<"\t"<<b;
cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
swap(c,d);
cout<<"\nc and d after swaping :"<<c<<"\t"<<d;
}

void main()
{
int a,b;
float c,d;
clrscr();
cout<<"Enter A,B values(integer):";
cin>>a>>b;
cout<<"Enter C,D values(float):";
cin>>c>>d;
fun(a,b,c,d);
getch();
}
Output
Enter A, B values (integer): 10 20 Enter C, D values (float): 2.50 10.80 A and B before swapping: 10 20 A and B after swapping: 20 10 C and D before swapping: 2.50 10.80 C and D after swapping: 10.80 2.50

Key Takeaways & Official Reference for Function Template In C++

When practicing function template in c++, test edge cases carefully and keep your formatting consistent. For deeper reference, review the C++ Reference Documentation or continue with our For Loop in C++.

Found this guide helpful? Support Team StudyWarehouse! Buy Us a Coffee

Pardeep Patel

Hi!, I am Pardeep Patel, an Indian passport holder, Traveler, Blogger, Story Writer. I completed my M-Tech (Computer Science) in 2016. I love to travel, eat different foods from various cuisines, experience different cultures, make new friends and meet other.

Leave a Reply

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