Module 3. Lesson Proper
Module 3. Lesson Proper
Variable scope refers to the region or context within a program where a variable is
accessible or can be used. Understanding variable scope is crucial because it determines the
visibility and lifetime of a variable within a program.
1. Local Scope
Definition: A variable declared inside a function or a block (such as within {}) is said
to have local scope.
Visibility: The variable is only accessible within the block or function where it is
declared.
Lifetime: The variable exists only during the execution of the block or function. Once
the block or function exits, the variable is destroyed.
Example:
#include <iostream>
using namespace std;
void exampleFunction() {
int localVar = 10; // localVar is local to exampleFunction
cout << "Inside function: " << localVar << endl; // Accessible here
} // localVar is destroyed here
int main() {
exampleFunction();
// cout << localVar; // Error: localVar is not accessible here
return 0;
}
2. Global Scope
Definition: A variable declared outside all functions and blocks is considered to have
global scope.
Visibility: The variable is accessible from any part of the program after its
declaration.
Lifetime: The variable exists throughout the entire execution of the program and is
destroyed when the program terminates.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Inside main: " << globalVar << endl; // Accessible here
exampleFunction();
return 0;
}
Multiple Variables
In C++, you can work with multiple variables in various ways, depending on how and
where they are declared and used. Here's a detailed overview:
Example:
Note: Declaration: Multiple variables of the same type can be declared in a single statement.
Example:
#include <iostream>
using namespace std;
int main() {
int var = 20; // Local variable in main function
{
int var = 30; // Block-scoped variable
cout << "Block scope var: " << var << endl; // Outputs 30
}
cout << "Local scope var: " << var << endl; // Outputs 20
cout << "Global scope var: " << ::var << endl; // Outputs 10 using scope resolution operator
return 0;
}
Note: Scope: Multiple variables can exist in different scopes with the same name (global, local,
block), leading to shadowing.
3. Variable Shadowing
When a local variable has the same name as a global variable, the local variable
"shadows" the global variable within its scope. The global variable can still be accessed
using the scope resolution operator ::.
Example:
#include <iostream>
using namespace std;
void exampleFunction() {
int x = 10; // Local variable shadows global x
cout << "Local x: " << x << endl; // Outputs 10
cout << "Global x: " << ::x << endl; // Outputs 5 using scope resolution operator
}
int main() {
exampleFunction();
return 0;
}
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20, c = 30;
int sum = a + b + c; // Sum of multiple variables
int product = a * b * c; // Product of multiple variables
#include <iostream>
using namespace std;
int main() {
for (int i = 0, j = 10; i < 5; i++, j--) {
cout << "i: " << i << ", j: " << j << endl;
}
// i and j are not accessible here because their scope is limited to the for loop
return 0;
}
Example:
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
printSumAndProduct(a, b); // Passes two variables to the function
return 0;
}
Example:
#include <iostream>
using namespace std;
int main() {
int arr[3] = {1, 2, 3}; // Array of three integers
for (int i = 0; i < 3; i++) {
cout << "arr[" << i << "]: " << arr[i] << endl;
}
return 0;
}
Note: Arrays: Arrays manage multiple variables of the same type efficiently.
Variables, literals, and constants are fundamental concepts that allow you to store, manage,
and manipulate data within a program.
1. Variables
Definition
A variable in C++ is a named storage location in memory that can hold a value.
The value of a variable can be changed during the execution of a program.
Example:
#include <iostream>
using namespace std;
int main() {
int age; // Declaration of an integer variable
age = 25; // Initialization of the variable
int year = 2024; // Declaration and initialization in one statement
cout << "Age: " << age << endl;
cout << "Year: " << year << endl;
return 0;
}
Variable Types
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10; // Integer variable
float b = 5.5f; // Floating-point variable
double c = 3.14; // Double variable
char d = 'A'; // Character variable
bool e = true; // Boolean variable
return 0;
}
2. Literals
Definition
A literal is a constant value that is directly written into the code. It represents
fixed values that do not change during the execution of the program.
Types of Literals
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10; // Integer literal
float b = 5.5f; // Floating-point literal
char c = 'A'; // Character literal
const char* str = "Hello"; // String literal
bool d = true; // Boolean literal
return 0;
}
3. Constants
Definition
A constant in C++ is a variable whose value cannot be changed once it has been
initialized. Constants are used to protect data from being modified accidentally.
Declaring Constants
#include <iostream>
using namespace std;
int main() {
const int PI = 3.14159; // Declaring a constant using const
// PI = 3.14; // Error: PI is a constant and cannot be modified
cout << "PI: " << PI << endl;
return 0;
}
#include <iostream>
#define PI 3.14159 // Declaring a constant using #define
using namespace std;
int main() {
cout << "PI: " << PI << endl;
// PI = 3.14; // Error: PI is a constant and cannot be modified
return 0;
}
const Variables:
#define Constants: