Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    Functions and collections · Lesson 6 of 21

    Functions

    Defining and returning, default arguments, and overloading.

    9 minutes2 games

    A function is named code you call when needed. In C++ you write the type it returns before the name (int, double, or void for nothing) and the type of every parameter.

    C++
    #include <iostream>
    #include <string>
    
    double average(int total, int count) {
        if (count == 0) return 0;
        return static_cast<double>(total) / count;
    }
    
    void greet(const std::string& name, const std::string& greeting = "Welcome") {
        std::cout << greeting << ", " << name << "!\n";
    }
    
    int main() {
        std::cout << average(17, 4) << "\n";
        greet("Omar");
        greet("Nour", "Good morning");
        return 0;
    }

    greeting has a default value, so leaving it out gives "Welcome". Defaults must come last in the parameter list.

    Overloading: same name, different types

    C++
    #include <iostream>
    #include <string>
    
    int twice(int x) { return x * 2; }
    double twice(double x) { return x * 2; }
    std::string twice(const std::string& s) { return s + s; }
    
    int main() {
        std::cout << twice(21) << "\n";
        std::cout << twice(1.25) << "\n";
        std::cout << twice(std::string("ha")) << "\n";
        return 0;
    }

    twice(21)

    the compiler sees the type: int

    picks int twice(int)

    The choice happens at compile time, not while running.

    Important

    A function must be known before it's called. Either define it above main, or put a declaration (just the first line with ;) above and the definition below. That's exactly what .h files hold: declarations shared across many files.

    Complete the code

    Complete the function that returns the larger of two numbers.

    #include <iostream>
     larger(int a, int b) {
         a > b ? a : b;
    }
    int main() { std::cout << larger(3, 8) << "\n"; }

    Interview question

    How do function overloading and default arguments differ, and when does overloading cause trouble?

    Exercise

    Write bool isPrime(int n) and use it to print the primes from 2 to 30 on one line, separated by spaces.

    Expected output: 2 3 5 7 11 13 17 19 23 29

    C++
    #include <iostream>
    
    bool isPrime(int n) {
        
    }
    
    int main() {
        for (int i = 2; i <= 30; i++) {
            if (isPrime(i)) std::cout << i << " ";
        }
        std::cout << "\n";
        return 0;
    }

    Learn more from

    • learncpp: Introduction to functionslearncpp.com
    • learncpp: Introduction to function overloadinglearncpp.com
    • cppreference: Default argumentsen.cppreference.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousLoopsNextReferences: by value or by reference?
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.