Topics covered on Monday, 2010-08-30
Variable Scopes
All variables defined in C++ have a
scope. The
scope of a variable is the portion of the program within which it exists.
In general, the scope of a variable is the
smallest block that includes the definition of the variable.
For instance, in the program below, two variables are defined with the same name. The scope of the first
variable is the block from lines 14 to 27. The scope of the second
variable is the block from lines 19 to 24. Note how changing the value of the second
variable within its scope doesn't affect the value of the first
variable.
Demonstrate variable scope (scope1.cpp)
1 //
2 // scope1.cpp
3 //
4 // Program to demonstrate the scope of variables.
5 //
6 // Copyright 2010, Raj Mathur
7 //
8 #include <iostream>
9 #include <cstdlib>
10
11 using namespace std;
12
13 int main( int argc, char *argv[] )
14 {
15 int
16 variable;
17 variable = 10;
18 cout << "Before the block, the value of variable is " << variable << endl;
19 {
20 int
21 variable;
22 variable = 20;
23 cout << "In the block, the value of variable is " << variable << endl;
24 }
25 cout << "After the block, the value of variable is " << variable << endl;
26 exit(0);
27 }
Functions
The program below is a modification of
compute1.cpp. We move the common portion (prompting the user for the operation) out into its own function
get_operation that may be called any number of times.
Add or subtract two numbers using a function (compute2.cpp)
1 // compute2.cpp
2 //
3 // Prompt user for 2 numbers and and operation (+ or -) and
4 // print out result of the operation. Use a fixed function to
5 // get the operation from the user.
6 //
7 // Copyright (C) 2010, Raj Mathur
8 #include <iostream>
9 #include <cstdlib>
10 #include <cstring>
11
12 using namespace std;
13 // Functions (prototypes)
14 char get_operation(void);
15
16 int main(int argc, char *argv[])
17 {
18 char
19 op;
20 int
21 number_1 = 0;
22 int
23 number_2 = 0;
24 int
25 result;
26 // Get operation type (p == plus, m == minus)
27 op = get_operation();
28 while( op != 'q' )
29 {
30 // Get values to be used for computation
31 cout << "What is the first number? ";
32 cin >> number_1;
33 cout << "What is the second number? ";
34 cin >> number_2;
35 // Check operation and do it
36 if( op == 'p' )
37 {
38 result = number_1 + number_2;
39 }
40 else if( op == 'm' )
41 {
42 result = number_1 - number_2;
43 }
44 // Display it
45 cout << "The result is: " << result << endl;
46 // Get operation type (p == plus, m == minus)
47 op = get_operation();
48 }
49 cout << "OK, exiting..." << endl;
50 exit(0);
51 }
52 //
53 // get_operation
54 //
55 // Get 'p', 'm' or 'q' from the user and return that.
56 //
57 char get_operation(void)
58 {
59 char
60 value = ' ';
61 while( value != 'p' && value != 'm' && value != 'q' )
62 {
63 cout << "Enter (p)lus, (m)inus or (q)uit: ";
64 cin >> value;
65 }
66 return( value );
67 }
Line 14
declares the function
get_operation. All functions must be declared before they are used. Line 14 intimates the compiler that
get_operation takes no arguments and returns one
char value to the calling function.
Lines 57 through 67
define the function
get_operation. All function definitions make a new block. The value given in the
return statement is made available to the calling function.
Add or subtract two numbers using a more flexible function (compute3.cpp)
1 // compute3.cpp
2 //
3 // Prompt user for 2 numbers and and operation (+ or -) and
4 // print out result of the operation. Use a flexible function to get
5 // the operation from the user.
6 //
7 // Copyright (C) 2010, Raj Mathur
8 #include <iostream>
9 #include <cstdlib>
10 #include <cstring>
11
12 using namespace std;
13 //
14 // Functions (prototypes)
15 char get_operation(char *prompt, char *valid_chars);
16
17 int main(int argc, char *argv[])
18 {
19 char
20 op;
21 int
22 number_1 = 0;
23 int
24 number_2 = 0;
25 int
26 result;
27 // Get operation type (p == plus, m == minus)
28 op = get_operation("Enter (p)lus, (m)inus, (t)imes or (q)uit: ", "pmtq");
29 while( op != 'q' )
30 {
31 // Get values to be used for computation
32 cout << "What is the first number? ";
33 cin >> number_1;
34 cout << "What is the second number? ";
35 cin >> number_2;
36 // Check operation and do it
37 if( op == 'p' )
38 {
39 result = number_1 + number_2;
40 }
41 else if( op == 'm' )
42 {
43 result = number_1 - number_2;
44 }
45 // Display it
46 cout << "The result is: " << result << endl;
47 // Get operation type (p == plus, m == minus)
48 op = get_operation("Enter (p)lus, (m)inus or (q)uit: ", "pmq");
49 }
50 cout << "OK, exiting..." << endl;
51 exit(0);
52 }
53 //
54 // get_operation
55 //
56 // Get 'p', 'm' or 'q' from the user and return that.
57 //
58 char get_operation(char *prompt, char *valid_chars)
59 {
60 char
61 value = ' ';
62 while( strchr(valid_chars, value) == NULL )
63 {
64 cout << prompt;
65 cin >> value;
66 }
67 return( value );
68 }
Assignment
Make
compute4.cpp based on
compute1.cpp so that it takes 1, 2 or 3 command-line arguments.
- If one argument is given, it is treated as the operation code. The program should then prompt for the first and second numbers, do the operation and display the result.
- If two arguments are given, they are treated as the operation and the first number. The program should then prompt for the second number, do the operation and display the result.
- If three arguments are given, they are treated as the operation, the first number and the second number. The program should then not prompt for anything, do the operation and display the result.