LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 3 of 27

Functions and Default Arguments

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Programs & Examples

Functions and Default Arguments · Study material

Tower of Hanoi with recursion

To move n disks, move n − 1 disks to the spare peg, move the largest disk, then move the n − 1 disks on top of it. The recursion makes 2n − 1 moves.

#include <iostream>

int moves = 0;

void hanoi(int n, char from, char to, char via) {
    if (n == 0) return;
    hanoi(n - 1, from, via, to);              // move n-1 disks out of the way
    std::cout << "disk " << n << ": " << from << " -> " << to << '\n';
    ++moves;
    hanoi(n - 1, via, to, from);              // put them back on top
}

int main() {
    hanoi(3, 'A', 'C', 'B');
    std::cout << moves << " moves\n";        // always 2^n - 1
    return 0;
}

Output

disk 1: A -> C
disk 2: A -> B
disk 1: C -> B
disk 3: A -> C
disk 1: B -> A
disk 2: B -> C
disk 1: A -> C
7 moves

Prime numbers with small helper functions

is_prime tests divisors only up to √n; next_prime reuses it. Small functions with clear names make the main program read like a description.

#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 next_prime(int n) {
    while (!is_prime(++n)) {}
    return n;
}

int main() {
    std::cout << "primes below 40:";
    for (int i = 2; i < 40; ++i)
        if (is_prime(i)) std::cout << ' ' << i;
    std::cout << "\nnext prime after 100: " << next_prime(100) << '\n';
    return 0;
}

Output

primes below 40: 2 3 5 7 11 13 17 19 23 29 31 37
next prime after 100: 101

Returning several results with a struct

The stats function from the introduction returns minimum, maximum and average together; the caller unpacks them with auto [lo, hi, avg] = stats(marks);.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links