example2_function_scope.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

void functionA() {
    int a = 5;
    std::cout << "In functionA: a = " << a << std::endl;
}

void functionB() {
    int b = 10;
    std::cout << "In functionB: b = " << b << std::endl;
    // std::cout << "a = " << a << std::endl;  // This would cause a compilation error
}

int main() {
    functionA();
    functionB();
    
    // std::cout << "a = " << a << std::endl;  // This would cause a compilation error
    // std::cout << "b = " << b << std::endl;  // This would cause a compilation error
    
    return 0;
}
Back to context