Variable Scope in C++ language.

C++ Programming Tutorial

Variable Scope in C++ language.

There are three places, where variables can be declared.

  1. Local variables.

  2. Global variables.

  3. Formal parameters.

1.Local variables.

Variables that are declared inside a function or block are local variables. They are not used outside of a function.

void function1() { int x=10;//local variable }

2.Global variables

A variable that is declared outside the function or block is called a global variable. Any function can change the value of the global variable. It is available to all the functions.

int value=20;//global variable void function1() { int x=10;//local variable }
#include <iostream> using namespace std; // Global variable declaration: int p; int main () { // Local variable declaration: int x, y; // actual initialization x = 10; y = 20; p = x + y; cout << p; return 0; }

Initializing Local and Global Variables

A local variable is not initialized by the system, you must initialize it yourself. Global variables are initialized automatically by the system. They are.

Data Type Initialize











3.Formal parameters.

The identifier used in a method to stand for the value that is passed into the method by a caller.