Function

Programming

This program demonstrates how to define and call functions in C++. Functions allow code reuse and modularity.

Implementation

#include <iostream>

int add(int x, int y) {
    return x + y;
}

int main() {
    int a = 5, b = 3;
    std::cout << "a: " << a << std::endl;
    std::cout << "b: " << b << std::endl;
    std::cout << "Sum: " << add(a, b) << std::endl;
    return 0;
}

Function Definition

A function definition has the following syntax:

return_type function_name(parameter_list) {
    // function body
    return value;
}

Key Concepts

Output

a: 5
b: 3
Sum: 8