Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LocalVariables are variables that are declared within a function or block of code.
- Their scope is limited from the point of declaration to the end of the function or block in wich they are declared.
- Their lifetime is from entering the function/block to the termination of the function/block or easily said to the end of the function/block.
- For example:
- int main()
- {
- string hey = "Hey!"; //Declaration
- cout << hey << endl; //This will print Hey! on the screen.
- system("pause");
- }
- int anotherFunctionHere()
- {
- cout << hey << endl; //But if we wanted to use that variable declared in int main() function it wouldn't work in int
- anotherFunctionHere because it's local.
- }
- GlobalVariables are variables declared outside of any function. Global variables are accessible in every scope.
- Global variables persist in memory for the duration of the program.
- For example:
- #include <iostream>
- #include <string>
- #include <math.h>
- #include <math.c>
- using namespace std;
- string coolStringName = "Hola!"; // As you can see the variable is declared outside of a function. So it doesn't belong anywhere
- //unless you use it in a function :)
- int main()
- {
- cout << "blablabla" << endl;
- cout << "Epic code written here!" << endl;
- }
- int coolAwesomeCode()
- {
- cout << "more blablabla" << endl;
- cout << "i'm getting bored of typing blablabla" << endl;
- cout << coolStringName << endl; //And know this will work :)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement