</>
Skip to content
C++ lessons (23/42)

C++ — Functions

Basic functions

#include <iostream>
using namespace std;

// Declaration
int add(int a, int b);

// Definition
int add(int a, int b) {
    return a + b;
}

int main() {
    cout << "3 + 4 = " << add(3, 4) << endl;
    return 0;
}

Default arguments

#include <iostream>
#include <string>
using namespace std;

void greet(string name, string greeting = "Hello") {
    cout << greeting << ", " << name << "!" << endl;
}

int main() {
    greet("Alice");           // Hello, Alice!
    greet("Bob", "Hi");      // Hi, Bob!
    return 0;
}

Function overloading

Multiple functions with the same name but different parameters:

#include <iostream>
using namespace std;

int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
string add(string a, string b) { return a + b; }

int main() {
    cout << add(3, 4) << endl;           // 7
    cout << add(3.14, 2.72) << endl;     // 5.86
    cout << add(string("Hello"), string(" World")) << endl;
    return 0;
}

Inline functions

#include <iostream>
using namespace std;

inline int square(int x) { return x * x; }

int main() {
    cout << "5² = " << square(5) << endl;
    return 0;
}

Pass by value vs reference

#include <iostream>
using namespace std;

void byValue(int x) { x = 100; }
void byReference(int &x) { x = 100; }
void byConstReference(const int &x) {
    cout << "Value: " << x << endl;
    // x = 100; // Error: const reference
}

int main() {
    int a = 5;
    byValue(a);
    cout << "After byValue: " << a << endl; // 5

    byReference(a);
    cout << "After byReference: " << a << endl; // 100

    byConstReference(a);

    return 0;
}

Lambda expressions (C++11)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    // Basic lambda
    auto greet = []() { cout << "Hello!" << endl; };
    greet();

    // With parameters
    auto add = [](int a, int b) { return a + b; };
    cout << "3 + 4 = " << add(3, 4) << endl;

    // With capture
    int factor = 3;
    auto multiply = [factor](int x) { return x * factor; };
    cout << "5 × 3 = " << multiply(5) << endl;

    // Capture by reference
    int counter = 0;
    auto increment = [&counter]() { counter++; };
    increment();
    increment();
    cout << "Counter: " << counter << endl;

    // With STL
    vector<int> nums = {5, 2, 8, 1, 9};
    sort(nums.begin(), nums.end(), [](int a, int b) {
        return a > b; // Descending
    });
    for (int n : nums) cout << n << " ";
    cout << endl; // 9 8 5 2 1

    return 0;
}

Return type deduction

#include <iostream>
using namespace std;

// auto return type (C++14)
auto multiply(int a, int b) {
    return a * b;
}

// Trailing return type
auto divide(double a, double b) -> double {
    return a / b;
}

int main() {
    cout << multiply(3, 4) << endl;
    cout << divide(10.0, 3.0) << endl;
    return 0;
}

constexpr functions

#include <iostream>
using namespace std;

constexpr int factorial(int n) {
    return (n <= 1) ? 1 : n * factorial(n - 1);
}

int main() {
    // Compile-time evaluation
    constexpr int f5 = factorial(5);
    cout << "5! = " << f5 << endl; // 120

    // Runtime evaluation
    int n = 10;
    cout << "10! = " << factorial(n) << endl;

    return 0;
}

std::function

#include <iostream>
#include <functional>
using namespace std;

int add(int a, int b) { return a + b; }

int main() {
    // Wrap any callable
    function<int(int, int)> op;

    op = add;
    cout << op(3, 4) << endl;

    op = [](int a, int b) { return a * b; };
    cout << op(3, 4) << endl;

    return 0;
}

Mini Practice

Write C++ code that:

  1. Overloads a function for int and double
  2. Uses a lambda to sort a vector of strings by length
  3. Captures variables by reference in a lambda
  4. Uses constexpr for a compile-time calculation

Up Next

In the next lesson, you'll learn about Classes — object-oriented programming in C++.

Related Topics

Frequently Asked Questions about Functions

What is Functions in C++?

Functions is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Functions?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Functions.

Why is Functions important in C++?

Functions is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.