Lesson 2

Welcome to the lesson on *functions templates* in C++! Our goal is to understand how to design functions that work flexibly with different data types, enhancing code reusability and efficiency.

Ever wondered how to write one function to handle different data types? This is where `templates`

are useful. By the end of this lesson, you'll be adept at creating and using `templates`

in C++.

Let's start with the basics: what are `template`

functions? A `template`

function is a blueprint for a function that can work with any data type. This enables you to write one function and use it with different types of variables.

For example, let's find the maximum of two objects. You could write separate functions for integers, floating-point numbers, and strings, but that's inefficient. Instead, a `template`

function can handle all these cases with one piece of code.

Here it is, the `findMax`

function template, which finds the maximum of two values:

C++`1#include <iostream> 2#include <string> 3 4// A template function to find the maximum of two values 5template <typename T> 6T findMax(T a, T b) { 7 return (a > b) ? a : b; 8} 9 10int main() { 11 std::cout << "Max of 3 and 7: " << findMax(3, 7) << std::endl; // Max of 3 and 7: 7 12 std::cout << "Max of 3.5 and 2.1: " << findMax(3.5, 2.1) << std::endl; // Max of 3.5 and 2.1: 3.5 13 std::cout << "Max of 'apple' and 'banana': " << findMax(std::string("apple"), std::string("banana")) << std::endl; // Max of 'apple' and 'banana': banana 14 return 0; 15}`

In this code:

- We declare a template with
`template <typename T>`

. `T`

is a placeholder for the type provided when the function is called.- The
`findMax`

function compares two values of type`T`

and returns the greater one.

When calling `findMax`

, the compiler deduces the type `T`

based on the arguments provided.

Next, let's see `swapValues`

, a `template`

function that swaps two variable values:

C++`1#include <iostream> 2#include <string> 3 4// A template function to swap two values 5template <typename T> 6void swapValues(T &a, T &b) { 7 T temp = a; 8 a = b; 9 b = temp; 10} 11 12int main() { 13 int x = 10, y = 20; 14 swapValues(x, y); 15 std::cout << "Swapped integers: " << x << " and " << y << std::endl; // Swapped integers: 20 and 10 16 17 double p = 5.5, q = 10.5; 18 swapValues(p, q); 19 std::cout << "Swapped doubles: " << p << " and " << q << std::endl; // Swapped doubles: 10.5 and 5.5 20 21 std::string s1 = "hello", s2 = "world"; 22 swapValues(s1, s2); 23 std::cout << "Swapped strings: " << s1 << " and " << s2 << std::endl; // Swapped strings: world and hello 24 25 return 0; 26}`

In this code:

- We define a template for
`swapValues`

with`template <typename T>`

. - The
`swapValues`

function takes two references of type`T`

and swaps their values using a temporary variable.

Just like `findMax`

, the compiler determines the type `T`

from the arguments.

Templates offer a streamlined alternative to function overloading. With overloading, you achieve similar functionality by defining multiple versions of a function, each specific to a type, but this leads to redundancy.

For example, you might overload `findMax`

like this:

C++`1#include <string> 2 3int findMax(int a, int b) { 4 return (a > b) ? a : b; 5} 6 7double findMax(double a, double b) { 8 return (a > b) ? a : b; 9} 10 11std::string findMax(std::string a, std::string b) { 12 return (a > b) ? a : b; 13} 14 15int main() {return 0;}`

While functional, this approach is cumbersome compared to the template version. Templates reduce redundancy and make maintenance easier. Changes to the function only need to be made once.

To demonstrate a more complex and meaningful example of a `template`

function that you might encounter in real work, let's look at a function that finds the sum of all elements in a collection, such as a `std::vector`

. This example showcases how templates can be applied to data structures.

Here is a `template`

function to calculate the sum of elements in a `std::vector`

:

C++`1#include <iostream> 2#include <vector> 3 4// A template function to compute the sum of all elements in a vector 5template <typename T> 6T sumVector(const std::vector<T>& vec) { 7 T sum = 0; 8 for (const T& elem : vec) { 9 sum += elem; 10 } 11 return sum; 12} 13 14int main() {return 0;}`

We define a template for `sumVector`

with `template <typename T>`

. The `sumVector`

function takes a vector with elements of type `T`

, denoted as `const std::vector<T>&`

, as input and calculates the sum of its elements using a for-each loop. The function returns the sum, which is of the same type as the elements in the vector.

Let's see an example of usage:

C++`1#include <iostream> 2#include <vector> 3 4// A template function to compute the sum of all elements in a vector 5template <typename T> 6T sumVector(const std::vector<T>& vec) { 7 T sum = 0; 8 for (const T& elem : vec) { 9 sum += elem; 10 } 11 return sum; 12} 13 14int main() { 15 std::vector<int> intVector = {1, 2, 3, 4, 5}; 16 std::cout << "Sum of integer vector: " << sumVector(intVector) << std::endl; // Output: 15 17 18 std::vector<double> doubleVector = {1.1, 2.2, 3.3, 4.4, 5.5}; 19 std::cout << "Sum of double vector: " << sumVector(doubleVector) << std::endl; // Output: 16.5 20 21 return 0; 22}`

When calling `sumVector`

, the compiler deduces the type `T`

from the vector's element type.

This example demonstrates how template functions can be effectively used with data structures, providing a reusable and type-safe way to operate on collections of elements.

To recap, `template`

functions, offer flexibility and reusability in C++. We covered:

- What
`template`

functions are and how they work. - Specific examples of
`template`

functions (`findMax`

and`swapValues`

). - Comparison between templates and function overloading.

Using `template`

functions effectively helps you write clean, efficient, and maintainable code.

Now that we've covered the theory and provided examples of **templates functions**, it's time to practice. The upcoming session will involve hands-on exercises creating and using `template`

functions to solve various problems. This practice will solidify your understanding and proficiency with **functions** in C++. Let's get started!