---
title: "Function template power(base, exp)"
subject: "Object Oriented Programming"
chapter: 9
key: "semester_3/object_oriented_programming:power-function-template"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/power-function-template
site: BCT Engineering
---

# Function template power(base, exp)

Object Oriented Programming · Chapter 9: Templates

- Asked in: 2068 Chaitra, 6 marks
- 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/power-function-template
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-9

## Question

Write a function template for the function power() which has two parameters base and exp and returns base^exp. The type of base is the parameter to the template and exp is int. If the exp is negative, then it must be converted to its positive equivalent. For example 2³ and 2⁻³ must both return 8.

## Short answer

Template function power(T base, int exp) returns base^exp. If exp<0, set exp=-exp. Compute result by multiplying base exp times. For 2^3 and 2^-3, result is 8; for 1.5^2, 2.25.

## Answer

The base type `T` is the template parameter and `exp` is an `int`. For a negative exponent, the sign is removed as the question requires (so 2⁻³ also returns 8).

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

template <class T>
T power(T base, int exp) {
    if (exp < 0)
        exp = -exp;              // convert to positive equivalent
    T result = 1;
    for (int i = 0; i < exp; i++)
        result *= base;
    return result;
}

int main() {
    cout << power(2, 3) << endl;        // 8
    cout << power(2, -3) << endl;       // 8
    cout << power(1.5, 2) << endl;      // 2.25
    cout << power(2.0f, -2) << endl;    // 4
    return 0;
}
```

**Output**

```text
8
8
2.25
4
```

Here `power(2, 3)` makes `T = int`, and `power(1.5, 2)` makes `T = double`. If `exp` is 0 the loop does not run and the result is 1.

## Check yourself

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

1. In power(T base, int exp), which is the template parameter?
   A. The type of base
   B. The type of exp
   C. The return statement
   D. The number of loops
   Answer: A. T is the type of base and the result; exp is a plain int. (Key point: Template parameter)

2. How does the function handle a negative exp as the question requires?
   A. It returns 1 / result
   B. It changes exp to -exp
   C. It returns 0
   D. It throws an error
   Answer: B. The sign is removed so that 2^-3 also gives 8. (Key point: Negative exponent)

3. What does power(5, 0) return and why?
   A. 0, because the loop does not run
   B. 5
   C. 1, because the loop does not run and result starts at 1
   D. An error
   Answer: C. result is initialised to 1 and the loop executes 0 times. (Key point: Zero exponent)

## 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×
- [Importance of function template](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-importance) — Object Oriented Programming, chapter 9, asked 2×
- [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: 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
- [Cube of int, float, double by overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acube-overloaded-program) — Object Oriented Programming, chapter 3
- [LandMeasure class and sum function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Alandmeasure-class-sum) — Object Oriented Programming, chapter 4

## Related in other subjects

- [Equivalent Y-parameter of parallel two-ports](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_circuit_theory%3Aequivalent-y-parameter-parallel-two-ports) — Electrical Circuit Theory, chapter 7, asked 3×
- [Equivalent Z-parameter of three series two-ports](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_circuit_theory%3Aequivalent-z-parameter-three-series-two) — Electrical Circuit Theory, chapter 7, asked 3×
- [Equivalent T from Y parameters of series two-ports](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_circuit_theory%3Aequivalent-t-parameter-two-y-networks) — Electrical Circuit Theory, chapter 7
- [Positive XOR equals negative XNOR](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Apositive-xor-negative-xnor-define) — Digital Logic, chapter 2, asked 3×
