Local vs Global Variables in C++
In the C++ programming language, parameter variables are used to store and process user-defined, declared data and information within a function program. These variables’ scope and lifetime depend entirely on where they are declared within the function, their behavior, and their intended purpose. Variables in a function program can be divided into local and global variables. Each parameter variable declared within a function has its own unique features and functions, including its scope, lifetime, and visibility.

So, let’s explore the concepts of local variables and global variables in C++ programming.
Local Variable Concept in C++.
Local variables in a function program are user-defined program variables declared within a custom user-defined function (or a block of program source code). These declared local variables can only be accessed and managed from within the declared function or block. Once execution of a local function variable exits the scope of the function or block, the local variable automatically ceases to exist.
Important features and functions of local variables.
- Scope – Local variables are limited to the function or block in which they are declared or defined as a parameter variable.
- Lifetime – Local variables are created when a user-defined function is called, and the program terminates automatically when the function exits.
- Visibility – Local variables in a user-defined function can only be accessed and managed from within the function or block in which they are declared or defined.
- Storage – Local variables are stored in stack order within a function in most functional programming cases.
Syntax of a Local Variable.
return_type function_name() {
int localPara; // integer local variable defined here
// you can write your custom code here using the localPara variable
}
Example of a local variable in C++.
#include <iostream>
using namespace std;
void preview() {
int localPara = 4; // here we define a local variable inside the function
cout << “here is the local variable parameter – ” << localPara << endl;
}
int main() {
preview(); // here it is calling the function that uses for the local variable value
// here user defined localVar cannot be accessed
return 0;
}
Local variable explanation.
- In the example given here, localPara is a local integer variable defined, declared inside the preview() function. localPara can only be accessed and managed from inside the preview() function. If you try to access preview() outside the function (e.g., in main()), a compilation error is displayed to the C++ user because localPara is a local variable outside the scope.
Global Variable Concept in C++.
Global variables in a function program are declared and defined outside of all functions. Global variables are typically defined at the top of a function program. Remember, global variables in a function program can be accessed and managed globally from any part of the program, including the area within the function itself.
Important features and functions of global variables.
- Scope – Global variable scope in a function program means that once declared, global variables can be accessed and managed from anywhere in the program.
- Lifetime – Global variables are created at program startup and automatically disappear when the program terminates.
- Visibility – Global variables are accessible and manageable within all functions and blocks within a function program.
- Storage − Global variables are stored or held in a special memory area called a data segment in the function.
Syntax of global variables.
int globalPara = 7; // This is a global variable method declared outside of any function.
return_type function_name() {
// Here you can create your own custom global variable code to access globalPara element.
}
Example of global variables.
#include <iostream>
using namespace std;
int globalPara = 7; // outside the function integer Global variable define
void preview() {
cout << “The universal global variable value is – ” << globalPara << endl;
}
int main() {
preview(); // here it Calling the function that uses the global variable method
globalPara = 9; // here we modify the global variable value
preview(); // here it is Calling the function again
return 0;
}
global variables explanation.
- In the example given here, globalPara is defined as a global variable in the function. It is declared as a global variable outside the main() and preview() functions. Because of this, it can be accessed and modified by both functions. Here you can see that when the value of globalPara is modified in main(), this modification is also displayed in the preview() function.
Combined Local and Global Variables Function Example.
#include <iostream>
using namespace std;
// Global variable with data name defined here
int data = 70;
void preview()
{
// Local variable declared inside the function
int data = 40;
cout << “Local variable inside preview() local function – ” << data << endl;
cout << “Global variable inside preview() global function – ” << ::data << endl;
}
int main()
{
// Local variable declare inside the main function
int data = 15;
cout << “Local variable inside the main() function – ” << data << endl;
cout << “Global variable inside the main() funtion – ” << ::data << endl;
preview();
return 0;
}
Detail explanation of Local vs Global Variables in c++
| Each Aspect | Local Variables features | Global Variables features |
| Each Scope | Function declares local variable use Limited to the function/block in which they are declared or define by programmer. | Global variable is Accessible throughout the entire program or anywhere in the declare function. |
| Lifetime features | Local variable Exists or remain only during the function call in program. | Global variable Exists or use throughout the complete function program execution. |
| Visibility in program | Local variable Visible only within the function/block they are declared. | Global variable Visible throughout the complete program inside or outside program. |
| Memory storage | Local variable Stored in the stack (temporary memory) order to process. | Global variable Stored in the data segment (persistent memory) order. |
| Initialization method | Local variable Must be explicitly initialized in function. | Global variable Automatically initialized to 0 (if not set) or define. |
| Modification allowed | Local variable Can be modified within their function body when need. | Global variable Can be modified by any function in the complete program. |
