Skip to content
Laviyx Learn

Try

C++ Functions Practice

Practise writing C++ functions, passing by value and by reference, overloading, default arguments, recursion and static variables.

Beginner7 problemsUpdated

Review first: Functions, Parameters: Value and Reference, Overloading and Default Arguments, Scope and Lifetime, Recursion

  1. 1.A prime-checking function

    Beginner

    Write bool is_prime(int n) and use it to print every prime number from 1 to 30 on one line.

    Show hint

    Numbers below 2 are not prime. Otherwise, try divisors d while d * d <= n.

    Show solution
    C++
    #include <iostream>
    
    bool is_prime(int n) {
        if (n < 2) return false;
        for (int d = 2; d * d <= n; ++d) {
            if (n % d == 0) return false;
        }
        return true;
    }
    
    int main() {
        for (int i = 1; i <= 30; ++i) {
            if (is_prime(i)) std::cout << i << ' ';
        }
        std::cout << '\n';
    }
    Output
    2 3 5 7 11 13 17 19 23 29
  2. 2.Swap with references

    Beginner

    Write void swap_values(int& a, int& b) that swaps two integers, and use it to swap x = 4 and y = 9.

    Show hint

    With reference parameters, the function changes the caller's variables. You need a temporary variable.

    Show solution
    C++
    #include <iostream>
    
    void swap_values(int& a, int& b) {
        int temp = a;
        a = b;
        b = temp;
    }
    
    int main() {
        int x = 4;
        int y = 9;
        swap_values(x, y);
        std::cout << x << ' ' << y << '\n';
    }
    Output
    9 4
  3. 3.Overloaded area

    Beginner

    Write two overloads of area: one taking a double radius (circle, using 3.14159) and one taking two doubles (rectangle). Print area(2.0) and area(3.0, 4.5).

    Show hint

    Overloads share a name but must differ in their parameter lists.

    Show solution
    C++
    #include <iostream>
    
    double area(double radius) {
        return 3.14159 * radius * radius;
    }
    
    double area(double width, double height) {
        return width * height;
    }
    
    int main() {
        std::cout << area(2.0) << ' ' << area(3.0, 4.5) << '\n';
    }
    Output
    12.5664 13.5
  4. 4.Default arguments

    Beginner

    Write void greet(const std::string& name, const std::string& greeting = "Hello") and call it with "Asha" and with "Ravi", "Welcome".

    Show hint

    A default argument is used when the caller leaves that argument out.

    Show solution
    C++
    #include <iostream>
    #include <string>
    
    void greet(const std::string& name, const std::string& greeting = "Hello") {
        std::cout << greeting << ", " << name << "!\n";
    }
    
    int main() {
        greet("Asha");
        greet("Ravi", "Welcome");
    }
    Output
    Hello, Asha!
    Welcome, Ravi!
  5. 5.Recursive factorial

    Beginner

    Write a recursive long long factorial(int n) and print factorial(10) and factorial(0).

    Show hint

    The base case is n <= 1, which returns 1. Otherwise return n * factorial(n - 1).

    Show solution
    C++
    #include <iostream>
    
    long long factorial(int n) {
        if (n <= 1) return 1;
        return n * factorial(n - 1);
    }
    
    int main() {
        std::cout << factorial(10) << ' ' << factorial(0) << '\n';
    }
    Output
    3628800 1
  6. 6.Recursive sum

    Beginner

    Write a recursive function int sum_from(const std::vector<int>& v, std::size_t i) that returns the sum of the elements from index i to the end. Print the sum of {4, 8, 15, 16, 23, 42}.

    Show hint

    When i == v.size() there is nothing left, so return 0. Otherwise return v[i] + sum_from(v, i + 1).

    Show solution
    C++
    #include <iostream>
    #include <vector>
    
    int sum_from(const std::vector<int>& v, std::size_t i) {
        if (i == v.size()) return 0;
        return v[i] + sum_from(v, i + 1);
    }
    
    int main() {
        std::vector<int> v = {4, 8, 15, 16, 23, 42};
        std::cout << sum_from(v, 0) << '\n';
    }
    Output
    108
  7. 7.Count the calls

    Beginner

    Write int next_id() that returns 1 on its first call, 2 on its second, and so on, without a global variable. Call it three times.

    Show hint

    A static local variable keeps its value between calls.

    Show solution
    C++
    #include <iostream>
    
    int next_id() {
        static int id = 0;
        return ++id;
    }
    
    int main() {
        int a = next_id();
        int b = next_id();
        int c = next_id();
        std::cout << a << ' ' << b << ' ' << c << '\n';
    }
    Output
    1 2 3

    Each call is stored in its own variable before printing, so the order of the results does not depend on the order in which the expression is evaluated.