---
title: "Importance of function template"
subject: "Object Oriented Programming"
chapter: 9
key: "semester_3/object_oriented_programming:function-template-importance"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/function-template-importance
site: BCT Engineering
---

# Importance of function template

Object Oriented Programming · Chapter 9: Templates

- Asked in: 2070 Asar, 2 marks; 2070 Chaitra, 4 marks (asked 2 times)
- Video: [C++ FUNCTIONS (2025) - What are generic functions and templates? PROGRAMMING TUTORIAL](https://www.youtube.com/watch?v=qrJjFN4Igfw) — CodeBeauty
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/function-template-importance
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-9

## Question

Explain the importance of function template with suitable example.

## Short answer

Function templates allow one generic function to work with any type, avoiding multiple overloads. They provide code reuse, type safety, and maintainability. Example: template<class T> void swapValues(T& a, T& b){ T temp=a; a=b; b=temp; } works for int, float, etc. Compiler generates type-specific versions.

## Answer

A **function template** defines one generic function for any data type, using a type parameter `T`. Its importance:

- **Code reuse** – one definition replaces many overloaded functions that differ only in data type.
- **Less effort and fewer errors** – logic is written and debugged once.
- **Type safety** – unlike macros or `void*`, the compiler checks types.
- **Easy maintenance** – a change in logic is made in one place.
- **No run-time cost** – the compiler generates the needed version at compile time.

#### Example
Without templates we need `swap` for `int`, `float`, `char`, etc. With a template:

```cpp
#include <iostream>
using namespace std;

template <class T>
void swapValues(T &a, T &b) {
    T temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 5, y = 9;
    float p = 1.5f, q = 2.5f;
    swapValues(x, y);          // T = int
    swapValues(p, q);          // T = float
    cout << x << " " << y << endl;   // 9 5
    cout << p << " " << q << endl;   // 2.5 1.5
    return 0;
}
```

One function serves both `int` and `float`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the main importance of a function template?
   A. It makes functions run faster at run time
   B. It removes the need for main()
   C. One definition replaces many overloaded functions differing only in type
   D. It allows functions without parameters only
   Answer: C. A single generic definition serves all data types, giving code reuse. (Key point: Code reuse)

2. How are templates better than macros or void* for generic code?
   A. They avoid compiling
   B. They use more memory
   C. They allow any type mixing
   D. The compiler checks types
   Answer: D. Templates keep type checking, whereas macros and void* do not. (Key point: Type safety)

3. In the swapValues template, why are the parameters T &a, T &b?
   A. So the original variables are exchanged
   B. So the function returns two values
   C. So T becomes int
   D. So the call is faster than a macro
   Answer: A. Reference parameters let the function modify the caller's variables so the swap is visible in main(). (Key point: swapValues example)

## Related in Object Oriented Programming

- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
- [Need of template; function template overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Awhy-template-function-overloading) — Object Oriented Programming, chapter 9
- [Function template power(base, exp)](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apower-function-template) — Object Oriented Programming, chapter 9
- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9
