Scope of Variables – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

scope of variablesScope of Variables | Global and Local Variables
Object Oriented Programming(OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Scope of Variables
Let us discuss scope of variables in C++ programming. A variable can be either of global or local scope. A global variable is a variable declared in the main body of the C++ source code, outside all the functions. Global variables can be called from anywhere in the code, even inside functions, whenever it is after its declaration.

The local variable is one declared within the body of a function or a block. To illustrate the scope of global variable and local variable, let us look at the figure below; The scope of local variables is limited to the block enclosed in braces ‘{}’ where they are declared. For example, if they are declared at the beginning of the body of a function (like in function main), their scope is between its declaration point and the end of that function.

scope variables in C++ program

In the example above, this means that if another function existed in addition to main, the local variables, declared in main could not be accessed from the other function and vice versa.

(Visited 561 times, 1 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag :

Post a Comment

Your email is never published nor shared. Required fields are marked *


Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary