Skip to main content
A scope is a region of the program and broadly speaking there are three places, where variables can be declared −
  • Inside a function or a block which is called local variables,
  • In the definition of function parameters which is called formal parameters.
  • Outside of all functions which is called global variables.
We will learn what is a function and it's parameter in subsequent chapters. Here let us explain what are local and global variables.

Local Variables

Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables:
#include <iostream>
using namespace std;
 
int main () {
   // Local variable declaration:
   int a, b;
   int c;
 
   // actual initialization
   a = 10;
   b = 20;
   c = a + b;
 
   cout << c;
 
   return 0;
}

Output

30

Global Variables

Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:
#include <iostream>
using namespace std;
 
// Global variable declaration:
int g;
 
int main () {
   // Local variable declaration:
   int a, b;
 
   // actual initialization
   a = 10;
   b = 20;
   g = a + b;
 
   cout << g;
 
   return 0;
}

Output

30
A program can have same name for local and global variables but value of local variable inside a function will take preference. For example:
#include <iostream>
using namespace std;
 
// Global variable declaration:
int g = 20;
 
int main () {
   // Local variable declaration:
   int g = 10;
 
   cout << g;
 
   return 0;
}
When the above code is compiled and executed, it produces the following result:
10

Initializing Local and Global Variables

When a local variable is defined, it is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system when you define them as follows:
Data TypeInitializer
int0
char'\0'
float0
double0
pointerNULL
It is a good programming practice to initialize variables properly, otherwise sometimes program would produce unexpected result.

Comments

Popular posts from this blog

In this chapter, we will see how to enable remote desktop application. It is important because this enables us to work remotely on the server. To do this, we have the following two options. For the first option, we have to follow the steps given below. Step 1  − Go to Start → right click “This PC” → Properties. Step 2  − On Left side click “Remote Setting”. Step 3  − Check radio button “Allow Remote connection to this computer” and Check box “Allow connection only from computers running Remote Desktop with Network Level Authentication (recommended)” → click “Select Users”. Step 4  − Click Add. Step 5  − Type user that you want to allow access. In my case, it is administrator → click OK. For the  second option , we need to follow the steps given below. Step 1  − Click on “Server Manage” → Local Server → click on “Enable” or Disable, if it is Disabled.

HaydarOrac

Virtualization is one of the most important technology feature and Microsoft has invested on this and the responsible role is called as Hyper-V. Let us now see how to install the Hyper-V Role, for doing this we should follow the steps given below. Step 1  − To Install DNS role go to “Server Manager” → Manage → Add Roles and Features. Step 2  − Click Next. Step 3  − Select the  Role-based  or  feature-based installation  option → click Next. Step 4  − I will install a Local Hyper-V role as it will Select a server from the server pool → Next. Step 5  − From the Roles lists, check the Hyper-V Server role → click Add Features on the popup windows which show up and then → Next. Step 6  − Click Next. Step 7  − Choose your server’s physical network adapters that will take part in the virtualization → Next. Step 8  − Under  Migration , leave the default settings →  Next . Step 9  − Choose the...