Lecture 18 Examples
Example 1: Reference and value parameters.
#include<iostream>
usingnamespace std;
void addFirst(int& first, int& second);
void doubleFirst(int one, int two);
void squareFirst(int& ref, int val);
int main ()
{
int num = 5;
cout <num< endl; //Line 1
addFirst(num, num);//Line 2
cout <num < endl; //Line 3
doubleFirst(num, num);//Line 4
cout < num < endl; //Line 5
squareFirst(num, num);//Line 6
cout <num < endl; //Line 7
return 0;
}
void addFirst(int& first, int& second)
{
cout < "first = " < first < ", second = " < second < endl;//Line 8
first = first + 2;//Line 9
cout < " first = " < first < ", second = " < second < endl;//Line 10
second = second * 2;//Line 11
cout < " first = " < first < ", second = " < second < endl;//Line 12
}
void doubleFirst(int one, int two)
{
cout < "one = " < one < ", two = " < two < endl; //Line 13
one = one * 2;//Line 14
cout < "one = " < one< ", two = " < two < endl; //Line 15
two = two + 2;//Line 16
cout < "one = " < one < ", two = " < two < endl; //Line 17
}
void squareFirst(int& ref, int val)
{
cout < "ref = "< ref < ", val = " < val < endl; //Line 18
ref = ref * ref;//Line 19
cout < "ref = "< ref < ", val = " < val < endl; //Line 20
val = val + 2;//Line 21
cout < "ref = "< ref < ", val = " < val < endl; //Line 22
}
Example 2 Reference and value parameters
#include<iostream>
usingnamespace std;
int t;
void funOne(int& a, int& x);
int main()
{
int num1, num2;
num1 = 10;//Line 1
num2 = 20;//Line 2
t = 15;//Line 3
cout < "num1 = " < num1 < ", num2 = "< num2 < ", and t = " < t< endl;//Line 4
funOne(num1, t);//Line 5
cout < "num1 = "< num1 < ", num2 = " < num2 < ", and t = " < t< endl; //Line 6
return 0;//Line 7
}
void funOne(int& a, int& x)
{
int z;
z = a + x;//Line 8
cout < "a = " < a < ", x = " < x< ", z = " < z < ", and t = " < t < endl; //Line 9
x = x + 5;//Line 10
cout < "a = " < a < ", x = " < x< ", z = " < z < ", and t = " < t < endl; //Line 11
a = a + 12; //Line 12
cout < "a = " < a < ", x = " < x< ", z = " < z < ", and t = " < t < endl; //Line 13
t = t + 13;//Line 14
cout < "a = " < a < ", x = " < x< ", z = " < z < ", and t = " < t < endl; //Line 15
}
Example 3 Static and automatic variables
#include<iostream>
usingnamespace std;
void test();
int main()
{
int count;
for (count = 1; count <= 5; count++)
test();
return 0;
}
void test()
{
staticint x = 0;
int y = 10;
x = x + 2;
y = y + 1;
cout <"x = " < x < " and y = "< y < endl;
}
Example 4:external variable
#include<iostream>
usingnamespace std;
void one();
void two();
int main()
{
int s;
externint w;
s = 10;
cout < "s = " < s < endl;
one();
two();
w = w + s;
cout < "In main external w = " < w < endl;
return 0;
}
void one()
{
externint w;
w = 15;
cout < "In one w = " < w < endl;
}
int w;
void two()
{
int z;
z = 12;
w = w + z;
cout < "In two z = " < z < endl;
cout < "In two w = " < w < endl;
}
Example 5Scope of an identifier
#include<iostream>
usingnamespace std;
constdouble rate=10.50;
int z;
double t;
void one(int x, char y);
void two(int a, int b, char x);
void three(int one, double y, int z);
int main()
{
int num,first;
double x, y, z;
char name, last;
...
...
return 0;
}
void one(int x, char y)
{
...
}
int w;
void two(int a, int b, char x)
{
int count;
...
}
void three(int one, double y, int z)
{
char ch;
int a;
...
//Block four
{
int x;
char a;
...
}//end Block four
...
}
Page 1