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;
int a=5, b=20, c=15;
//globals a,b,c have scope here, and anywhere else in this program.
void brainmelting(int x, int &y, int &z)
{
//globals a,b,c have scope here
//copy of x is made, which starts its scope here and ends at the end of this function
//reference of y and z is used here. It will be viewable by main() since we passed the address (reference)
int b=10; //This local b will now take over. Global b is now "ignored"!
x=a+c;
y=z+5+b;
c=x+y;
//The copy of x is now destroyed, since it was passed-by-value
//The values of y and z are preserved and "stored" into whatever names they were in main().
}
int main()
{
//Global variables a, b, c have scope here
int a=100; //This variable is now used as a. Global "a" is now ignored.
brainmelting(a,b,c); //pass these variables; once we get inside the function, we don't care what they were called. They could've been donkey, giraffe, and platypus for all we care. We're just passing the integers, not the "name"
brainmelting(b,c,a);
return 0;
}
| |