Computer Science 1620 - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Science 1620

Description:

its lifetime is until the end of the program #include iostream using ... double result; int main() { cout 'Factorial of 5 = ' factorial(5) endl; ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 26
Provided by: Kev995
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 1620


1
Computer Science 1620
  • Function Scope Global Variables

2
  • 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

3

include ltiostreamgt using namespace
std double factorial(int x) double result
1 for (int y 1 y lt x y) result
y return result int main() cout ltlt
"Factorial of 5 " ltlt factorial(5) ltlt endl
cout ltlt "Result of computation " ltlt result ltlt
endl return 0
Variable result's lifetime
Compiler error result is being used outside of
its scope.
4

include ltiostreamgt using namespace
std double factorial(int x) double result
1 for (int y 1 y lt x y) result
y return result int main() cout ltlt
"Factorial of 5 " ltlt factorial(5) ltlt endl
cout ltlt "Result of computation " ltlt x ltlt
endl return 0
Variable x's lifetime
Compiler error x is being used outside of its
scope.
5
  • 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

6

include ltiostreamgt using namespace
std double factorial(int n) double result
1 for (int y 1 y lt n y) result
y return result int choose(int n, int r)
double result factorial(n) / (factorial(r)
factorial(n-r)) return static_castltintgt(resul
t) int main() cout ltlt "5 choose 2 " ltlt
choose(5, 2) ltlt endl return 0
These variables are distinct, even though they
have the same name.
7
  • 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

8

include ltiostreamgt using namespace std
double result double factorial(int x)
result 1.0 for (int y 1 y lt x y)
result y return result int
main() cout ltlt "Factorial of 5 " ltlt
factorial(5) ltlt endl cout ltlt "Result of
computation " ltlt result ltlt endl return
0
Variable result's lifetime
This is ok result is being used within its
scope.
9

include ltiostreamgt using namespace std
double factorial(int x) result 1.0
for (int y 1 y lt x y) result y
return result double result int
main() cout ltlt "Factorial of 5 " ltlt
factorial(5) ltlt endl cout ltlt "Result of
computation " ltlt result ltlt endl return
0
Compiler error result being used outside of its
scope
Variable result's lifetime
10
  • Global Variables Name Resolution
  • suppose you have a global variable and local
    variable with the same name
  • which variable takes priority?

Does this output a 1 or a 2?
int a 1 int main() int a 2
cout ltlt "a " ltlt a ltlt endl return 0
11
  • Global Variables Name Resolution
  • Rule local variables hide global variables with
    the same name

This outputs a 2
int a 1 int main() int a 2
cout ltlt "a " ltlt a ltlt endl return 0
12
  • 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

This outputs a 2
int a 1 int main() int a 2
cout ltlt "a " ltlt a ltlt endl return 0
13
  • 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

This outputs a 1
int a 1 int main() int a
2 cout ltlt "a " ltlt a ltlt endl
return 0
14
  • 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 ()

This outputs a 2
int a 1 int main() int a 2
cout ltlt "a " ltlt a ltlt endl return 0
15
  • 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 ltlt "a " ltlt a ltlt endl return 0
This outputs a 1
16
  • 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

17

include ltiostreamgt using namespace
std double factorial(int x) double result
1 for (int y 1 y lt x y) result
y return result int main() cout ltlt
"Factorial of 5 " ltlt factorial(5) ltlt endl
return 0
18

include ltiostreamgt using namespace std int
x double result void factorial() result
1.0 for (int y 1 y lt x y) result
y int main() x 5 factorial()
cout ltlt "Factorial of 5 " ltlt result ltlt endl
return 0
19
  • 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

20

include ltiostreamgt using namespace std int
x double result void factorial() result
1.0 for (int y 1 y lt x y) result
y int main() x 5 factorial()
cout ltlt "Factorial of 5 " ltlt result ltlt endl
return 0
No obvious associativity with function call
21

include ltiostreamgt using namespace
std double factorial(int x) double result
1 for (int y 1 y lt x y) result
y return result int main() cout ltlt "5
choose 2 " ltlt factorial(5)/
(factorial(2) factorial(5-2)) ltlt endl
return 0
22

include ltiostreamgt using namespace std int
x double result void factorial() result
1.0 for (int y 1 y lt x y) result
y int main() cout ltlt "5 choose 2 "
double choose 1.0 x 5 factorial()
choose result x 2 factorial()
choose / result x 3 factorial()
choose / result cout ltlt "5 choose 2 " ltlt
choose ltlt endl return 0
23
  • 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)

24
Write a program that reads the radius of a
sphere, and outputs its widest circumference, its
volume, and its surface area
include ltiostreamgt using namespace std int
main() const double PI 3.14 double
r cout ltlt "Please enter the radius of the
sphere (m) " cin gtgt r cout ltlt
"Circumference (m) " ltlt 2 PI r ltlt endl
cout ltlt "Volume (m3) " ltlt 4.0 / 3.0
PIrrr ltltendl cout ltlt "Surface Area (m2)
" ltlt 4 PI rr ltlt endl return
0
25
Write a program that reads the radius of a
sphere, and outputs its widest circumference, its
volume, and its surface area
include ltiostreamgt using namespace std
const double PI 3.14 int main() double
r cout ltlt "Please enter the radius of the
sphere (m) " cin gtgt r cout ltlt
"Circumference (m) " ltlt 2 PI r ltlt endl
cout ltlt "Volume (m3) " ltlt 4.0 / 3.0
PIrrr ltltendl cout ltlt "Surface Area (m2)
" ltlt 4 PI rr ltlt endl return 0
Write a Comment
User Comments (0)
About PowerShow.com