What term is used to describe the portion of a program in which a variable can be accessed?

In general, the scope is defined as the extent up to which something can be worked with. In programming also the scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with. There are mainly two types of variable scopes: 

  1. Local Variables
  2. Global Variables

What term is used to describe the portion of a program in which a variable can be accessed?

Now let’s understand each of the scope at a greater detail: 
 

Local Variables

Variables defined within a function or block are said to be local to those functions.  

  • Anything between ‘{‘ and ‘}’ is said to inside a block.
  • Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.
  • Declaring local variables: Local variables are declared inside a block.

C++

#include

using namespace std;

void func()

{  

    int age=18;   

}

int main()

{

    cout<<"Age is: "<

    return 0;

}

Output: 

Error: age was not declared in this scope

The above program displays an error saying “age was not declared in this scope”. The variable age was declared within the function func() so it is local to that function and not visible to portion of program outside this function. 

Rectified Program : To correct the above error we have to display the value of variable age from the function func() only. This is shown in the below program: 

C++

#include

using namespace std;

void func()

{  

    int age=18;

    cout<

}

int main()

{

    cout<<"Age is: ";

    func();

    return 0;

}

Output: 

Age is: 18

Global Variables

As the name suggests, Global Variables can be accessed from any part of the program.

  • They are available through out the life time of a program.
  • They are declared at the top of the program outside all of the functions or blocks.
  • Declaring global variables: Global variables are usually declared outside of all of the functions and blocks, at the top of the program. They can be accessed from any portion of the program.

C++

#include

using namespace std;

int global = 5;

void display()

{

    cout<

}

int main()

{

    display();

    global = 10;

    display();

}

Output: 

5 10

In the program, the variable “global” is declared at the top of the program outside all of the functions so it is a global variable and can be accessed or updated from anywhere in the program.  

What if there exists a local variable with the same name as that of global variable inside a function?

Let us repeat the question once again. The question is : if there is a variable inside a function with the same name as that of a global variable and if the function tries to access the variable with that name, then which variable will be given precedence? Local variable or Global variable? Look at the below program to understand the question:  

C++

#include

using namespace std;

int global = 5;

int main()

{  

    int global = 2;

    cout << global << endl;

}

Look at the above program. The variable “global” declared at the top is global and stores the value 5 where as that declared within main function is local and stores a value 2. So, the question is when the value stored in the variable named “global” is printed from the main function then what will be the output? 2 or 5?

  • Usually when two variable with same name are defined then the compiler produces a compile time error. But if the variables are defined in different scopes then the compiler allows it.
  • Whenever there is a local variable defined with same name as that of a global variable then the compiler will give precedence to the local variable

How to access a global variable when there is a local variable with same name?

What if we want to do the opposite of above task. What if we want to access global variable when there is a local variable with same name? 
To solve this problem we will need to use the scope resolution operator. Below program explains how to do this with the help of scope resolution operator. 

C++

#include

using namespace std;

int x = 0; 

int main()

{

  int x = 10;

  cout << "Value of global x is " << ::x;

  cout<< "\nValue of local x is " << x; 

  return 0;

}

Output: 

Value of global x is 0 Value of local x is 10

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


What programming term is used to indicate the part of a program in which a variable may be accessed?

A local variable's scope is the part of a program in which the variable may be accessed and is only visible to the statements in the variable's scope.

What type of variable is visible to every module and the entire program?

Introduction to Programming 03.

What are the pieces of data that are passed into a module called?

is any piece of data that is passed into a module when the module is called. parameter; is a variable that receives an argument that is passed into a module. You just studied 9 terms!

What are variables used for in programs quizlet?

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.