-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableScope.cpp
More file actions
24 lines (17 loc) · 952 Bytes
/
VariableScope.cpp
File metadata and controls
24 lines (17 loc) · 952 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
#include <iostream>
int myNum = 3; //global variable //causes error when same named variable is passed to a function that calls this
void printNum(); // function definition,
int main()
{
//Local variables = declared inside a function or block {}
//Global variables = declared outside of all functions
int myNum = 1; //local variables
printNum(); //function call, ignores local variable value, function assumes global variable value
std::cout << "The number is: " << myNum << '\n';// a function will first look for local variables then global so this would print 1
std::cout << ::myNum << '\n'; //prints global due to the scope resolution operator (::) which tells the compiler to look for the global variable
return 0;
}
void printNum() // function definition, doesnt need myNum as a argument due to it being global
{
std::cout << "The number is: " << myNum << '\n'; // prints the number passed to the function
}