0% found this document useful (0 votes)
31 views13 pages

CS101

The document discusses C++ functions and enumerations. It defines an enumeration called Status with constants CONTINUE, WON, LOST. A craps game simulator is presented using these statuses. Functions are used to roll dice and determine the game outcome. The document also covers storage classes like auto, static, and extern and how they affect variable scope within and across functions. Identifier scope rules are explained for file, function, and block levels.

Uploaded by

sibghat ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views13 pages

CS101

The document discusses C++ functions and enumerations. It defines an enumeration called Status with constants CONTINUE, WON, LOST. A craps game simulator is presented using these statuses. Functions are used to roll dice and determine the game outcome. The document also covers storage classes like auto, static, and extern and how they affect variable scope within and across functions. Identifier scope rules are explained for file, function, and block levels.

Uploaded by

sibghat ullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Functions - II

Dr. Shahab Ansari


Example: A Game of Chance and Introducing enum

• Enumeration - set of integers with identifiers


enum typeName {constant1, constant2…};
– Constants start at 0 (default), incremented by 1
– Unique constant names
– Example:
enum Status {CONTINUE, WON, LOST};
• Create an enumeration variable of type typeName
– Variable is constant, its value may not be reassigned
Status enumVar; // create variable
enumVar = WON; // set equal to WON
enumVar = 1; // ERROR
Example: A Game of Chance and Introducing
enum(II)
• Enumeration constants can have values pre-set
enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL,
AUG, SEP, OCT, NOV, DEC};
– Starts at 1, increments by 1
• Craps simulator rules
– Roll two dice
• 7 or 11 on first throw, player wins
• 2, 3, or 12 on first throw, player loses
• 4, 5, 6, 8, 9, 10
– value becomes player's "point"
– player must roll his point before rolling 7 to win
1// Fig. 3.10: fig03_10.cpp

2// Craps

3#include <iostream>

5using std::cout;

6using std::endl;

8#include <cstdlib>

10 #include <ctime>

11

12 using std::time;

13

14 int rollDice( void ); // function prototype

15

16 int main()

17 {

18

19
enum Status { CONTINUE, WON, LOST };

int sum, myPoint;


Notice how
20 Status gameStatus; the enum is
21
defined
22 srand( time( 0 ) );

23 sum = rollDice(); // first roll of the dice

24

25 switch ( sum ) {

26 case 7:

27 case 11: // win on first roll

28 gameStatus = WON;

29 break;

30 case 2:

31 case 3:

32 case 12: // lose on first roll

33 gameStatus = LOST;

34 break;
35 default: // remember point
36 gameStatus = CONTINUE;
37 myPoint = sum;
38 cout << "Point is " << myPoint << endl;
39 break; // optional
40 }
41
42 while ( gameStatus == CONTINUE ) { // keep rolling
43 sum = rollDice();
44
45 if ( sum == myPoint ) // win by making point
46 gameStatus = WON;
47 else
48 if ( sum == 7 ) // lose by rolling 7
49 gameStatus = LOST;
50 }
51
52 if ( gameStatus == WON )
53 cout << "Player wins" << endl;
54 else
55 cout << "Player loses" << endl;
56
57 return 0;
58 }
59
60 int rollDice( void )

61 {

62 int die1, die2, workSum;

63

64 die1 = 1 + rand() % 6;

65 die2 = 1 + rand() % 6;

66 workSum = die1 + die2;

67 cout << "Player rolled " << die1 << " + " << die2

68 << " = " << workSum << endl;

69

70 return workSum;

71 }

Player rolled 6 + 5 = 11
Player wins

Player rolled 6 + 5 = 11
Player wins

Player rolled 4 + 6 = 10
Point is 10
Player rolled 2 + 4 = 6
Player rolled 6 + 5 = 11
Player rolled 3 + 3 = 6
Player rolled 6 + 4 = 10
Player wins

Player rolled 1 + 3 = 4
Point is 4
Player rolled 1 + 4 = 5
Player rolled 5 + 4 = 9
Player rolled 4 + 6 = 10
Player rolled 6 + 3 = 9
Player rolled 1 + 2 = 3
Player rolled 5 + 2 = 7
Player loses
Storage Classes
• Storage class specifiers
– Storage class
• Where object exists in memory
– Scope
• Where object is referenced in program
– Linkage
• Where an identifier is known
• Automatic storage
– Object created and destroyed within its block
– auto
• Default for local variables.
• Example:
auto float x, y;
– register
• Tries to put variables into high-speed registers
– Can only be used with local variables and parameters
Storage Classes

• Static storage
– Variables exist for entire program execution
– static
• Local variables defined in functions
• Keep value after function ends
• Only known in their own function
– Extern
• Default for global variables and functions.
• Known in any function
Identifier Scope Rules
• File scope
– Defined outside a function, known in all functions
– Examples include, global variables, function definitions and
functions prototypes
• Function scope
– Can only be referenced inside a function body
– Only labels (start:, case:, etc.)
• Block scope
– Declared inside a block. Begins at declaration, ends at }
– Variables, function parameters (local variables of function)
– Outer blocks “hidden” from inner blocks if same variable name
• Function prototype scope
– Identifiers in parameter list
– Names in function prototype optional, and can be used anywhere
1// Fig. 3.12: fig03_12.cpp

2// A scoping example

3#include <iostream>

5using std::cout;

6using std::endl;

8void a( void ); // function prototype

9void b( void ); // function prototype

10 void c( void ); // function prototype

11

12 int x = 1; // global variable


x is different inside and
13 outside the block.
14 int main()

15 {

16 int x = 5; // local variable to main

17

18 cout << "local x in outer scope of main is " << x << endl;

19

20 { // start new scope

21 int x = 7;

22

23 cout << "local x in inner scope of main is " << x << endl;

24 } // end new scope

25

26 cout << "local x in outer scope of main is " << x << endl;

27

28 a(); // a has automatic local x

29 b(); // b has static local x

30 c(); // c uses global x


local x in outer scope of main is 5
local x in inner scope of main is 7
31 a(); // a reinitializes automatic local x
local x in outer scope of main is 5
32 b(); // static local x retains its previous value

33 c(); // global x also retains its value

34
35 cout << "local x in main is " << x << endl;

36

37 return 0;

38 } Local automatic
39
variables are created
40 void a( void )

41 {
and destroyed each time
42 int x = 25; // initialized each time a is called a is called.
43

44 cout << endl << "local x in a is " << x

45 << " after entering a" << endl; local x in a is 25 after entering a
46 ++x; local x in a is 26 before exiting a
47 cout << "local x in a is " << x

48 << " before exiting a" << endl;

49 }

50

51 void b( void )

52 {

53 static int x = 50; // Static initialization only


Local static variables
54 // first time b is called. are not destroyed
55 cout << endl << "local static x is " << x
when the function
56 << " on entering b" << endl;

57 ++x;
ends.
local static x is 50 on entering b
58 cout << "local static x is " << x local static x is 51 on exiting b
59 << " on exiting b" << endl;

60 }

61 Global variables are


62 void c( void )
always accessible.
63 {

64 cout << endl << "global x is " << x


Function c references the
65 << " on entering c" << endl; global x. global x is 1 on entering c
66 x *= 10;
global x is 10 on exiting c
67 cout << "global x is " << x << " on exiting c" << endl;

68 }
local x in outer scope of main is 5
local x in inner scope of main is 7
local x in outer scope of main is 5
 
local x in a is 25 after entering a
local x in a is 26 before exiting a
 
local static x is 50 on entering b
local static x is 51 on exiting b
 
global x is 1 on entering c
global x is 10 on exiting c
 
local x in a is 25 after entering a
local x in a is 26 before exiting a
 
local static x is 51 on entering b
local static x is 52 on exiting b
 
global x is 10 on entering c
global x is 100 on exiting c
local x in main is 5
References
Dietal and Dietal : How to Program C++
3rd Edition

You might also like