Topic 23 of 91 4. Functions in C++ Interactive Tutorial

C++ Functions: Declarations, Definitions, Parameters, and Return Values

16 mins read Last updated: 26 Sep 2026 23,100 views

Direct Answer & Core Concept

Functions in C++ encapsulate reusable logic. A function declaration (prototype) specifies the function name, return type, and parameter signature. Definitions contain the implementation body. Arguments can be passed by Value (copied), by Pointer (address copied, nullable), or by Reference (alias, no copying, non-nullable). Functions can return values by value, reference, or auto deduction.

Detailed Explanation & Mechanics

1. Parameter Passing Modes:

Architecture Diagram
PARAMETER PASSING COMPARISON:
┌─────────────────┬──────────────────┬──────────────────┬─────────────────────────────┐
│ Mechanism       │ Syntax           │ Copies Data?     │ Nullable / Rebindable?      │
├─────────────────┼──────────────────┼──────────────────┼─────────────────────────────┤
│ Pass by Value   │ void f(int x)    │ YES (New copy)   │ Independent local copy      │
│ Pass by Ref     │ void f(int& x)   │ NO (Alias)       │ Never NULL, cannot rebind   │
│ Const Ref       │ void f(const T&) │ NO (Read-only)   │ Fast, safe for large objects│
│ Pass by Pointer │ void f(int* x)   │ NO (Copies addr) │ Can be nullptr, rebindable  │
└─────────────────┴──────────────────┴──────────────────┴─────────────────────────────┘

Runnable Code Example: C++ Functions

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <string>

// Function declaration with default argument
void configureDevice(const std::string& name, int timeoutMs = 5000);

// Pass by reference to modify caller state
void swapValues(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

// Function definition
void configureDevice(const std::string& name, int timeoutMs) {
    std::cout << "Device [" << name << "] configured with timeout: " << timeoutMs << " ms
";
}

int main() {
    configureDevice("Sensor-01"); // Uses default 5000ms
    configureDevice("Sensor-02", 2000);

    int x = 10, y = 20;
    swapValues(x, y);
    std::cout << "After swap: x=" << x << ", y=" << y << "
";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: Why should large objects (strings, vectors, classes) be passed by const reference?

A: Passing large objects by value creates a full deep copy on the stack, incurring memory allocation and CPU overhead. Passing by `const Type&` passes an efficient memory address without copying while guaranteeing the function cannot mutate the original object.
Q

Q: What are default arguments in C++ and where must they be declared?

A: Default arguments allow calling functions without specifying all parameters. They must be declared in the function declaration (header), and once a parameter has a default, all subsequent parameters to its right must also have defaults.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Pass small fundamental types (int, float, char) by value.
2. Pass large structs and classes by const Type&.
3. Use non-const reference Type& for out-parameters when returning multiple mutated values or use std::tuple/struct.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.