SCOPE OF A VARIABLE
Scope of a
variable
·
Part
of the program where the variable can be accessed or used.
·
Variable
scope determines its lifetime and visibility.
Types of Variable
Scope :-
1. Local Scope
- Declared
inside a block (e.g., within functions, loops, or conditional statements) .
- Lifetime: inside the block.
- Visibility: Only accessible within the
block where they are defined.
Syntax:
void functionName() {
int localVariable = 10; // Local variable
// Accessible only within this function
}
Example:
#include <iostream>
using namespace std;
void example() {
int x = 10; // Local variable
cout << "Local x: " << x << endl;
}
int main() {
example();
// cout << x; // Error: x
is not accessible here
return 0;
}
2. Global Scope
declared
outsideà all functions have global scope.
- Lifetime: Ã throughout the lifetime of
the program.
- Visibility: Ã Accessible by all functions in
the program (unless overridden by a local variable with the same name).
Syntax:
int
globalVariable = 100; // Global variable
void
functionName() {
// Accessible here
}
Example:
#include <iostream>
using namespace std;
int globalVar = 100; // Global variable
void display() {
cout << "Global variable: " << globalVar <<
endl;
}
int main() {
cout << "Accessing global variable in main: " <<
globalVar << endl;
display();
return 0;
}
3. Block Scope
- declared
within a block { }.
- accessible
only within that block.
- Lifetimeà Same as local variablesà destroyed after the block
ends.
Syntax:
{
int blockVariable = 50; // Block variable
// Accessible only within this block
}
Example:
#include <iostream>
using namespace std;
int main() {
{
int blockVar = 42; // Block scope
cout << "Block variable:
" << blockVar << endl;
}
// cout << blockVar; //
Error: blockVar is not accessible here
return 0;
}
4. Function Scope
- Use
in function parameters.
- Lifetimeà during the function execution.
- Visibility: Ã throughout the function.
Syntax :
Return_datatypes function_name( int a)
{
int
b,c;
c= a+b;
return c;
}
Example:
#include <iostream>
using namespace std;
void display(int num) { // num has function scope
cout << "Function parameter: " << num <<
endl;
}
int main() {
display(5);
return 0;
}
5. Static Scope
- Retain
value even after the block or function in which they are defined ends.
- Lifetimeà Exists for the entire duration
of the program.
- Visibility: Ã where they are declared.
Example:
#include <iostream>
using namespace std;
void countCalls() {
static int count = 0; // Static
variable
count++;
cout << "Function called " << count <<
" times" << endl;
}
int main() {
countCalls();
countCalls();
countCalls();
return 0;
}
6. Namespace Scope
- within
a namespace are accessible.
- Lifetime:Ã Exists throughout the program
execution.
- Visibility:Ã Accessible by specifying the
namespace or using the using directive.
Syntax:
namespace myNamespace {
int variable = 30; // Namespace variable
}
Example:
#include <iostream>
namespace myNamespace {
int num = 42; // Namespace scope
}
int main() {
std::cout << "Namespace variable: " <<
myNamespace::num << std::endl;
return 0;
}
7. File Scope
- Definition: Variables declared with the
static keyword at the global level are restricted to the file where they
are defined.
- Lifetime: Exists throughout the
program execution.
- Visibility: Limited to the file in which
it is declared.
Example (in a single file):
#include <iostream>
static int fileVar = 50; // File scope
void display() {
std::cout << "File scope variable: " << fileVar
<< std::endl;
}
int main() {
display();
return 0;
}
Summary of Scopes
|
Scope |
Where Declared |
Lifetime |
Visibility |
|
Local |
Inside a block |
Till the block ends |
Inside the block only |
|
Global |
Outside all functions |
Entire program execution |
Entire program |
|
Block |
Inside { } |
Till the block ends |
Inside the block only |
|
Function |
Function parameters |
Till the function executes |
Inside the function only |
|
Static |
Anywhere (local/global) |
Entire program execution |
Depends on declaration |
|
Namespace |
Inside a namespace |
Entire program execution |
Inside the namespace |
|
File |
Static at global level |
Entire program execution |
Current file only |
==============================================================
0 Comments