-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31_globalVariables.cpp
More file actions
35 lines (22 loc) · 855 Bytes
/
31_globalVariables.cpp
File metadata and controls
35 lines (22 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;
// global variable created
int global = 10;
int a(){
cout<<"Global variable in a: "<<global<<endl;
}
int b(){
cout<<"Global variable in b: "<<global<<endl;
global += 10;//changing value by reassigning
}
int main(){
// Global variables are the variables that has global scope all over the program and can be accessed by any function, class or scope. But we must avoid using global variables in many cases because through reassigning, its value can be easily changed.
//Global variables are defined above or outside the scope of int main();
a();//calling a function
b();//calling b function
cout<<"Global variable in main: "<<global<<endl;
{
cout<<"Global variable in undefined scope: "<<global<<endl;
}
return 0;
}