Introduction
The scope is one of the essential concepts in C++ programming. It is a way to limit the visibility of variables and functions to a particular part of the program.
The concept of scope in C++ is an article that gives a complete understanding of the subject With illustrative examples.
Scope in C++
Scope in C++ is the region of program text over which an identifier can be referenced or accessed. Some variables can be accessed from anywhere within the program.
Regardless of the other variables, they can only be accessed within a specific context and limits.
The traveler by plane at night and while looking out the window has a comprehensive view of the shining city, while the wanderer in the streets of Nafsh of this city has a limited view as the extension of Basra is obscured by the walls of high buildings. With this example, it is possible to understand the concept of scope.C++ offers two primary scopes levels. They are:
- Local scope or block scope
- Global scope
Local scope
A variable or function declared within a block will only be visible within that block. Curly braces limit the block.
Variables declared at the top level will have global scope, which means they can be seen from any point in your program.
A variable declared inside a function has local scope, meaning it can only be seen from inside that function.
Example
The code of the program
#include <iostream>using namespace std;void local_scope();void local_scope(int score) {int myScore {400}; cout << “nLocal myScore is: ” << myScore << ” in local_scope – version 1″ << endl;cout << endl;myScore=score;cout << “Local myScore is: ” << myScore << ” in local_scope – version 2″ << endl;cout << endl;cout << “Local myScore is: ” << myScore << ” in local_scope -version 3″ << endl;}int main() {local_scope(50);cout << endl;return 0;}
The code of the program
Global scope
Variables with the global scope are visible to all parts of your program, including other files and libraries you may use.
In other words, after the global identifier is declared, the global identifier will be visual to all aspects of the same program.
Example
The code of the program
This is a global variable. The variable is declared outside the function.#include<iostream>using namespace std;void global_scope();int myScore {100}; void global_scope() {cout << “nGlobal myScore is: ” << myScore<< ” in global_scope – start” << endl;myScore += 200;cout << endl;cout << “Global myScore is: ” << myScore << ” in global_scope – end” << endl;}int main() {global_scope();cout << endl;global_scope();cout << endl;return 0;}
The output of the program
Static local variables
Static local variables are declared within a function but outside any other function scope (like loops and if statements). These variables will retain their values from one execution to another because they were declared outside any further function scope.
Static local variables are made at the point definition and killed when the block is left.By default, local variables hold an automatic duration.
Example
The static_scope retains its value between calls.
The code of the program
#include <iostream>using namespace std;void static_scope();void static_scope() {static int myScore {1000}; cout << ” static scope myScore is: ” << myScore << ” in static_scope – version 1″ << endl;myScore *=5;cout << endl;cout << ” static scope myScore is: ” << myScore << ” in static_scope – version 2″ << endl;cout << endl;cout << ” static scope myScore is: ” << myScore << ” in static_scope – version 3″ << endl;}int main() {static_scope();cout << endl;return 0;}
The output of the program
Conclusion
Scope determines what parts of the program can access an object or variable. If there is no scope, then it means that any part of the program can access it.
If there is limited scope, then it means that only certain parts of the program have access to it, while other parts do not have access to it at all.
The concept of scope in C++ is fundamental because if you don’t understand how scope works, you might end up with errors that will make your code stop working correctly and even lead to security problems in your system.