Skip to content

Functions

About functions

These snippets are about the syntax for defining functions. They are not about how to achieve tasks with these functions. The tasks are just placeholders and the C++ STL already provides much better alternatives for most of these algorithms.

1
void show_menu();

1
2
3
4
5
6
void show_menu() {
    std::cout << "Options:\n"
                 "[0] Exit\n"
                 "[1] Sum\n"
                 "[2] Multiply\n";
}

1
2
3
4
// Call function 3 times
for (int i = 0; i < 3; ++i) {
    show_menu();
}

1
constexpr double pi() { return 3.14159265358979323846; }

1
std::cout << "π = " << pi() << '\n';

1
double power_three(double x) { return x * x * x; }

1
std::cout << "4.4^3 = " << power_three(4.4) << '\n';

1
2
3
4
5
6
7
double power(double x, int y) {
    double result = 1;
    for (int i = 0; i < y; ++i) {
        result *= x;
    }
    return result;
}

1
std::cout << "4.4^5 = " << power(4.4, 5) << '\n';

1
void times_two(int &x) { x *= 2; }

1
2
3
int x = 5;
times_two(x);
std::cout << "x = " << x << '\n'; // 10

1
2
3
4
5
6
// Always pass arrays by reference to avoid copying
void increment_all(std::array<int, 1000> &a, int increment_value = 1) {
    for (int &x : a) {
        x += increment_value;
    }
}

1
2
3
4
5
6
std::array<int, 1000> v{};
increment_all(v);
for (int i : v) {
    std::cout << i << " ";
}
std::cout << '\n';

1
2
3
4
5
increment_all(v, 10);
for (int i : v) {
    std::cout << i << " ";
}
std::cout << '\n';

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Parameters by constant reference
// - We don't want to copy
// - But we also don't want to change it
bool has_element(const std::array<int, 1000> &a, int element) {
    for (int x : a) {
        if (x == element) {
            return true;
        }
    }
    return false;
}

1
2
3
4
5
6
7
8
9
std::array<int, 1000> v{};
for (size_t i = 0; i < v.size(); ++i) {
    v[i] = i;
}
if (has_element(v, 400)) {
    std::cout << "Array v has the element 400" << '\n';
} else {
    std::cout << "Array v doesn't have the element 400" << '\n';
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
void minmax(int a, int b, int c, int &minimum, int &maximum) {
    if (a > b) {
        minimum = b;
        maximum = a;
    } else {
        minimum = a;
        maximum = b;
    }
    if (c > maximum) {
        maximum = c;
    }
    if (c < minimum) {
        minimum = c;
    }
    // values of external variables are set
}

1
2
3
4
int maximum;
int minimum;
minmax(5, 3, 8, minimum, maximum);
std::cout << "Minimum and maximum: " << minimum << ", " << maximum << '\n';

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
std::pair<int, int> minmax(int a, int b, int c) {
    int minimum;
    int maximum;
    if (a > b) {
        minimum = b;
        maximum = a;
    } else {
        minimum = a;
        maximum = b;
    }
    if (c > maximum) {
        maximum = c;
    }
    if (c < minimum) {
        minimum = c;
    }
    return std::make_pair(minimum, maximum);
}

1
2
std::pair<int, int> p = minmax(5, 3, 8);
std::cout << "Minimum and maximum: " << p.first << ", " << p.second << '\n';

1
2
auto [m1, m2] = minmax(5, 3, 8);
std::cout << "Minimum and maximum: " << m1 << ", " << m2 << '\n';

1
2
std::tie(m1, m2) = minmax(5, 3, 8);
std::cout << "Minimum and maximum: " << m1 << ", " << m2 << '\n';

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
void sort(int a, int b, int c, int &minimum, int &middle, int &maximum) {
    minmax(a, b, c, minimum, maximum);
    if (a != minimum && a != maximum) {
        middle = a;
        return;
    }
    if (b != minimum && b != maximum) {
        middle = b;
        return;
    }
    middle = c;
}

1
2
3
4
5
6
int minimum;
int middle;
int maximum;
sort(5, 3, 8, minimum, middle, maximum);
std::cout << "Sorting 5, 3, 8 = " << minimum << ", " << middle << ", "
          << maximum << '\n';

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
std::tuple<int, int, int> sort(int a, int b, int c) {
    auto [minimum, maximum] = minmax(a, b, c);
    if (a != minimum && a != maximum) {
        return std::make_tuple(minimum, a, maximum);
    }
    if (b != minimum && b != maximum) {
        return std::make_tuple(minimum, b, maximum);
    }
    return std::make_tuple(minimum, b, maximum);
}

1
2
3
std::tuple<int, int, int> t = sort(5, 3, 8);
std::cout << "Sorting 5, 3, 8: " << get<0>(t) << ", " << get<1>(t) << ", "
          << get<2>(t) << '\n';

1
2
3
auto [minimum3, middle3, maximum3] = sort(5, 3, 8);
std::cout << "Sorting 5, 3, 8: " << minimum3 << ", " << middle3 << ", "
          << maximum3 << '\n';

1
2
3
std::tie(minimum3, middle3, maximum3) = sort(5, 3, 8);
std::cout << "Sorting 5, 3, 8: " << minimum3 << ", " << middle3 << ", "
          << maximum3 << '\n';

1
2
3
4
5
6
7
// Pass pointers by value: https://youtu.be/xGDLkt-jBJ4?t=869
// As small as the reference and no other level of indirection
void pointer_to_cube(std::shared_ptr<int> p) {
    if (p) {
        *p = *p * *p * *p;
    }
}

1
2
3
std::shared_ptr<int> ptr = std::make_shared<int>(5);
pointer_to_cube(ptr);
std::cout << "5^3 = " << *ptr << '\n';

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
int fibonacci_iterative(int n) {
    if (n == 0) {
        return 0;
    } else if (n <= 2) {
        return 1;
    }
    int fib;
    int fib_previous = 1;
    int fib_previous_2 = 1;
    for (int i = 3; i <= n; ++i) {
        fib = fib_previous + fib_previous_2;
        fib_previous_2 = fib_previous;
        fib_previous = fib;
    }
    return fib;
}

1
2
3
4
5
6
7
int fibonacci_recursive(int n) {
    if (n > 2) {
        return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2);
    } else {
        return n == 0 ? 0 : 1;
    }
}

1
2
std::cout << "fibonacci_iterative(6) = " << fibonacci_iterative(6) << '\n';
std::cout << "fibonacci_recursive(6) = " << fibonacci_recursive(6) << '\n';

Share Snippets