Computer Science 1620 Function Scope & Global Variables
Functions and Scope recall that a variable's scope does not extend past the statement block in which it is defined therefore, a variable declared in a function can only be used in that function this also applies to parameter variables
#include using namespace std; double factorial(int x) { double result = 1; for (int y = 1; y <= x; y++) result *= y; return result; } int main() { cout << "Factorial of 5 = " << factorial(5) << endl; cout << "Result of computation = " << result << endl; return 0; } Variable result's lifetime Compiler error: result is being used outside of its scope.
#include using namespace std; double factorial(int x) { double result = 1; for (int y = 1; y <= x; y++) result *= y; return result; } int main() { cout << "Factorial of 5 = " << factorial(5) << endl; cout << "Result of computation = " << x << endl; return 0; } Variable x's lifetime Compiler error: x is being used outside of its scope.
Functions and Scope since a variable's scope doesn't exist past the function it is declared in, two functions can have the same variable name refers to two different variables
#include using namespace std; double factorial(int n) { double result = 1; for (int y = 1; y <= n; y++) result *= y; return result; } int choose(int n, int r) { double result = factorial(n) / (factorial(r) * factorial(n-r)); return static_cast (result); } int main() { cout << "5 choose 2 = " << choose(5, 2) << endl; return 0; } These variables are distinct, even though they have the same name.
Global Variables all variables so far have been declared within a function local variables also called automatic variables what happens if a variable is declared outside of a function variable becomes global its scope is any function defined after its declaration its lifetime is until the end of the program
#include using namespace std; double result; double factorial(int x) { result = 1.0; for (int y = 1; y <= x; y++) result *= y; return result; } int main() { cout << "Factorial of 5 = " << factorial(5) << endl; cout << "Result of computation = " << result << endl; return 0; } Variable result's lifetime This is ok; result is being used within its scope.
#include using namespace std; double factorial(int x) { result = 1.0; for (int y = 1; y <= x; y++) result *= y; return result; } double result; int main() { cout << "Factorial of 5 = " << factorial(5) << endl; cout << "Result of computation = " << result << endl; return 0; } Variable result's lifetime Compiler error: result being used outside of its scope
Global Variables & Name Resolution suppose you have a global variable and local variable with the same name which variable takes priority? int a = 1; int main() { int a = 2; cout << "a = " << a << endl; return 0; } Does this output a 1 or a 2?
Global Variables & Name Resolution Rule: local variables hide global variables with the same name int a = 1; int main() { int a = 2; cout << "a = " << a << endl; return 0; } This outputs a 2
Global Variables & Name Resolution Rule: local variables hide global variables with the same name local variables hide global variables until they go out of scope int a = 1; int main() { int a = 2; cout << "a = " << a << endl; return 0; } This outputs a 2
Global Variables & Name Resolution Rule: local variables hide global variables with the same name local variables hide global variables until they go out of scope int a = 1; int main() { { int a = 2; } cout << "a = " << a << endl; return 0; } This outputs a 1
Scope Operator unlike a local variable that's hidden, there is a way to acquire the value of a hidden global variable use the scope operator (::) int a = 1; int main() { int a = 2; cout << "a = " << a << endl; return 0; } This outputs a 2
Scope Operator unlike a local variable that's hidden, there is a way to acquire the value of a hidden global variable use the scope operator (::) tells C++ to look for variable in global space int a = 1; int main() { int a = 1; cout << "a = " << ::a << endl; return 0; } This outputs a 1
Global Variables vs. Parameters since global variables exist across functions, they can be used to pass information from function to function instead of using parameters and return types, just use global variables NOT RECOMMENDED
#include using namespace std; double factorial(int x) { double result = 1; for (int y = 1; y <= x; y++) result *= y; return result; } int main() { cout << "Factorial of 5 = " << factorial(5) << endl; return 0; }
#include using namespace std; int x; double result; void factorial() { result = 1.0; for (int y = 1; y <= x; y++) result *= y; } int main() { x = 5; factorial(); cout << "Factorial of 5 = " << result << endl; return 0; }
Global Variables vs. Parameters using global variables for parameter passing and return values is considered bad programming practice difficult to follow risk of overwriting parameters or return values risk of hiding global variable sometimes leads to longer code
#include using namespace std; int x; double result; void factorial() { result = 1.0; for (int y = 1; y <= x; y++) result *= y; } int main() { x = 5; factorial(); cout << "Factorial of 5 = " << result << endl; return 0; } No obvious associativity with function call
#include using namespace std; double factorial(int x) { double result = 1; for (int y = 1; y <= x; y++) result *= y; return result; } int main() { cout << "5 choose 2 = " << factorial(5)/ (factorial(2) * factorial(5-2)) << endl; return 0; }
#include using namespace std; int x; double result; void factorial() { result = 1.0; for (int y = 1; y <= x; y++) result *= y; } int main() { cout << "5 choose 2 = "; double choose = 1.0; x = 5; factorial(); choose *= result; x = 2; factorial(); choose /= result; x = 3; factorial(); choose /= result; cout << "5 choose 2 = " << choose << endl; return 0; }
Global Variables what are global values used for? named constants named constants are very often declared outside of a function, rather than inside of a function this way, all the functions can use the named constant external variables to be discussed later (if at all)
Write a program that reads the radius of a sphere, and outputs its widest circumference, its volume, and its surface area #include using namespace std; int main() { const double PI = 3.14; double r; cout << "Please enter the radius of the sphere (m): "; cin >> r; cout << "Circumference (m): " << 2 * PI * r << endl; cout << "Volume (m^3): " << 4.0 / 3.0 *PI*r*r*r <<endl; cout << "Surface Area (m^2): " << 4 * PI * r*r << endl; return 0; }
Write a program that reads the radius of a sphere, and outputs its widest circumference, its volume, and its surface area #include using namespace std; const double PI = 3.14; int main() { double r; cout << "Please enter the radius of the sphere (m): "; cin >> r; cout << "Circumference (m): " << 2 * PI * r << endl; cout << "Volume (m^3): " << 4.0 / 3.0 *PI*r*r*r <<endl; cout << "Surface Area (m^2): " << 4 * PI * r*r << endl; return 0; }