C++ Tutorial - 2

Functions, Vectors, and Efficient Iteration

5. Functions

Functions in C++ are crucial for encapsulating reusable code blocks, enhancing both modularity and maintainability in your programs.

Function Declaration

A function declaration specifies the function’s name, return type, and parameters. For example:

int add(int a, int b);

Function Definition

The function definition contains the executable code of the function. Here’s a simple function that adds two integers:

int add(int a, int b) { // Function definition
    return a + b;

Parameters and Return Types

Functions can have parameters (inputs) and a return type (output). The types of the parameters define what data the function expects, and the return type specifies what data the function returns. Default Parameters

Default parameters allow functions to be called with fewer arguments than they are defined with. Here’s an example:

int multiply(int a, int b = 2) {
    return a * b; // If b is not specified, it defaults to 2

Function Overloading

Function overloading allows you to have multiple functions with the same name but different parameter lists or types, enhancing readability and flexibility. I had a bit to much fun using this. While it can be helpfull, e.g. when you have the same functionality and just slightly different input, it might get a bit messy if you use it for different utils but name it the same (not that I haven’t done that).

int multiply(int a, int b = 2) {
    return a * b;

// Overloaded with a different type
double multiply(double a, double b = 2) {
    return a * b;

6. Introduction to Vectors

Vectors in C++ are dynamic arrays capable of resizing themselves automatically when elements are added or removed. They provide more flexibility than traditional arrays. Though those also exist in C++. Declaration and Initialization

Vectors can be declared and initialized in various ways:

std::vector<int> numbers; // Empty vector declaration
std::vector<int> nums = {1, 2, 3, 4, 5}; // Initialized with values
good-to-know Vectors use more memory than arrays to manage their dynamic sizing? It's a small price to pay for the convenience and safety they offer.

Manipulating Vectors

Vectors offer methods like push_back(), pop_back(), and size() to add elements, remove elements, and get the vector size:

numbers.push_back(6); // Adds '6' to the end of the vector
numbers.pop_back(); // Removes the last element

Swap two elements:

std::swap(nums[0], nums[4]); // Swapping first and last elements

Find the maximum element:

int maxElement = *max_element(nums.begin(), nums.end()); // Who's the biggest?

Reade here for more infos about vectors.

7. Range-Based For Loop with Vectors

Range-based for loops offer a simpler and more readable way to iterate over elements in containers like vectors. Syntax

Here’s how you can use a range-based for loop with vectors:

std::vector<int> nums = {1, 2, 3, 4, 5};
for (int element : nums) {
    std::cout << element << " "; // Prints each element of the vector

Pretty neat

good-to-know The range-based for loop was introduced in C++11. It's a great example of how C++ is evolving to make coding easier and more intuitive. Still some design choices of modern C++ are viewed quite critically
  • Consult the C++ Documentation for more details on Functions and Vectors.
  • Challenge yourself with projects that apply these concepts, like building a simple calculator or a basic to-do list.
Previous Tutorial <<<Part #1<<<
Next Tutorial >>>Link will be here when the third tutorial is out>>>