LABORATORY 8       Parameter Passing

 

Objective

 

This week in the lab we will be examining parameter passing. Understanding parameterized functions will enable you to design functions that correctly compute and return values to your program. Misuse of parameters is a common problem among programmersÑa problem that often results in unexpected errors.

 

Key Concepts

 

á      Programmer-defined functions

á      Invocation and flow of control

á      Value parameters

á      Reference parameters

á      const parameters

á      return statement

á      Local objects

 

 

GETTING STARTED

 

We will ask you a series of questions during this lab. Please download this answer form file in which you can record your answers to the various questions posed in this lab. Please answer each question before going on to the next section of the lab. When you are completely finished, submit your answers electronically. You won't need to download any files for this lab you can either type them in or cut and paste them from this page!

 

Up to this point your experience with functions has been fairly limited. However, you should not underestimate their usefulness in imposing logical structure upon programs. An important part of using functions entails the understanding of parameters. Parameters enable functions to be flexible. With parameters a single function can handle a variety of related tasksÑthe parameters will determine which tasks to do and which values to compute. Parameters have this important role because they are the primary interface between the function to be invoked and the calling program fragment. Parameters enable programmers to control the values or objects that pass in and out of a function.

 

Without parameters, we would be forced to rely on global objects as the sole means of communicating values between the various functions. However, we know that using global objects to share information is hazardous. Global objects make programs hard to trace and debug, since changing one global object could have ramifications throughout many different functions. Programs whose functions use parameters to share information are more modular and are easier to understand.

 

For each of the problems in this laboratory, perform the following activities

 

* Read the program, but do not run it!

* Trace through it by hand and predict the results of the program.

* Cut-and-paste or type in the code so that you can compile and execute it.

* Write down the observed results when running the program.

* If your answer disagrees with the observed results, go back and see why your answer was incorrect.  If you cannot figure out a problem, ask a laboratory instructor for assistance.

 

Helpful Hint: When you try to trace parameters by hand, it is useful to draw boxes for each object and then write the current value in the box. This method enables you to see what is going on with each parameter. It also enables you to cross out an old value and replace it with a new one every time an object is updated.

 

 

PROBLEM 1

 

 #include <iostream>

using namespace std;

void MyFunc() {

 

}

 

int main() {

int i = 10;

int j = 20;

 

MyFunc();

 

cout << "main: i = " << i << endl;

cout << "main: j = " << j << endl;

return 0;

}

 

Questions:

a.     Before you run the program, what do you predict as the output?

 

b.     Now run the actual program.  What was the observed output?

 

c.     If your predictions did not precisely match the observed output, explain the cause of the differences.

 

PROBLEM 2

 

What output is produced?

 

#include <iostream>

using namespace std;

 

void MyFunc(int i, int j) {

cout << "MyFunc: i = " << i << endl;

cout << "MyFunc: j = " << j << endl;

}

 

int main() {

int i = 10;

int j = 20;

 

MyFunc(i, j);

 

cout << "main: i = " << i << endl;

cout << "main: j = " << j << endl;

return 0;

}

 

 

Questions:

a.     Before you run the program, what do you predict as the output?

 

b.     Now run the actual program.  What was the observed output?

 

c.     If your predictions did not precisely match the observed output, explain the cause of the differences.

 

PROBLEM 3

 

#include <iostream>

using namespace std;

 

void MyFunc(int i, int j) {

i = i + j;

j = j + i;

 

cout << "MyFunc: i = " << i << endl;

cout << "MyFunc: j = " << j << endl;

}

 

int main() {

int i = 10;

int j = 20;

 

MyFunc(50, j);

MyFunc(i, 50);

 

cout << "main: i = " << i << endl;

cout << "main: j = " << j << endl;

 

return 0;

}

 

Questions:

a.     Before you run the program, what do you predict as the output?

 

b.     Now run the actual program.  What was the observed output?

 

c.      If your predictions did not precisely match the observed output, explain the cause of the differences.

 

PROBLEM 4

 

 

#include <iostream>

using namespace std;

 

void MyFunc(int i, int j) {

int temp;

 

temp = i;

i = j;

j = temp;

 

cout << "MyFunc: i = " << i << endl;

cout << "MyFunc: j = " << j << endl;

}

 

int main() {

int a = 10;

int b = 20;

 

MyFunc(a, b);

MyFunc(b, a);

 

cout << "main: a = " << a << endl;

cout << "main: b = " << b << endl;

return 0;

}

 

Questions:

a.     Before you run the program, what do you predict as the output?

 

b.     Now run the actual program.  What was the observed output?

 

c.      If your predictions did not precisely match the observed output, explain the cause of the differences.

 

PROBLEM 5

 

#include <iostream>

using namespace std;

void MyFunc(int &i, int &j) {

int temp;

 

temp = i;

i = 30;

j = temp;

 

cout << "MyFunc: i = " << i << endl;

cout << "MyFunc: j = " << j << endl;

}

 

int main() {

int a = 10;

int b = 20;

 

cout << "main: a = " << a << endl;

cout << "main: b = " << b << endl;

 

MyFunc(a, b);

MyFunc(b, a);

 

cout << "main: a = " << a << endl;

cout << "main: b = " << b << endl;

return 0;

}

 

Questions:

a.     Why would it be illegal to change the first call of function MyFunc() in the function main() to MyFunc(i, j)?

 

b.     Before you run the program, what do you predict as the output?

 

c.     Now run the actual program.  What was the observed output?

 

d.      If your predictions did not precisely match the observed output, explain the cause of the differences.

 

PROBLEM 6

.

#include <iostream>

using namespace std;

 

void MyFunc(int &i, int j) {

int temp;

 

temp = i;

i = 30;

j = temp;

 

cout << "MyFunc: i = " << i << endl;

cout << "MyFunc: j = " << j << endl;

}

 

int main() {

int a = 10;

int b = 20;

 

MyFunc(a, b);

 

cout << "main: a = " << a << endl;

cout << "main: b = " << b << endl;

return 0;

}

 

Questions:

a.     Before you run the program, what do you predict as the output?

 

b.     Now run the actual program.  What was the observed output?

 

c.      If your predictions did not precisely match the observed output, explain the cause of the differences.

 

d.     Why should it be illegal to change the call of MyFunc() in the function main() to MyFunc(10, b)?

 

e.     Why should it be legal to change the call of MyFunc() in the function main() to MyFunc(a, 20)?

 

PROBLEM 7

6.

#include <iostream>

using namespace std;

 

int MyFunc(int &i, int j) {

int temp;

temp = i;

i = 40;

j = temp;

cout << "MyFunc: i = " << i << endl;

cout << "MyFunc: j = " << j << endl;

return j;

}

 

int main() {

int a = 10;

int b = 20;

int c = 30;

 

c = MyFunc(b, b);

 

cout << "main: a = " << a << endl;

cout << "main: b = " << b << endl;

cout << "main: c = " << c << endl;

 

return 0;

}

 

Questions:

a.     Before you run the program, what do you predict as the output?

 

b.     Now run the actual program.  What was the observed output?

 

c.     If your predictions did not precisely match the observed output, explain the cause of the differences.

 

PROBLEM 8

 

#include <iostream>

using namespace std;

 

void MyFunc(int &i, int j, int k) {

int a = 100;

int b = 200;

 

i = k + a;

j = k + i;

k = a;

 

cout << "MyFunc: i = " << i << endl;

cout << "MyFunc: j = " << j << endl;

cout << "MyFunc: k = " << k << endl;

cout << "MyFunc: a = " << a << endl;

cout << "MyFunc: b = " << b << endl;

}

int main() {

int a = 10;

int b = 20;

 

MyFunc(b, a, b);

 

cout << "main: a = " << a << endl;

cout << "main: b = " << b << endl;

return 0;

}

 

Questions:

a.     Before you run the program, what do you predict as the output?

 

b.     Now run the actual program.  What was the observed output?

 

c.     If your predictions did not precisely match the observed output, explain the cause of the differences.

 

PROBLEM 9

 

#include <iostream>

using namespace std;

 

void MyFunc(const int i, int &j) {

int a = 100;

int b = 200;

 

a = b + j;

j = i + a;

 

cout << "MyFunc: i = " << i << endl;

cout << "MyFunc: j = " << j << endl;

cout << "MyFunc: a = " << a << endl;

cout << "MyFunc: b = " << b << endl;

}

 

int main() {

int a = 10;

int b = 20;

 

MyFunc(b, a);

 

cout << "main: a = " << a << endl;

cout << "main: b = " << b << endl;

 

return 0;

}

 

 

Questions:

a.     Before you run the program, what do you predict as the output?

 

b.     Now run the actual program.  What was the observed output?

 

c.     If your predictions did not precisely match the observed output, explain the cause of the differences.

 

d.     Why would it be illegal to add the statement i = a + b; to the function MyFunc()?

 

 

PROBLEM 10

 

#include <iostream>

using namespace std;

 

void MyFunc(int i, int j = 50) {

int a = 100;

int b = 200;

 

i = i + j + a + b;

 

cout << "MyFunc: i = " << i << endl;

cout << "MyFunc: j = " << j << endl;

cout << "MyFunc: a = " << a << endl;

cout << "MyFunc: b = " << b << endl;

}

 

int main() {

int a = 10;

int b = 20;

int c = 30;

 

MyFunc(a, b);

MyFunc(c);

MyFunc(0);

 

cout << "main: a = " << a << endl;

cout << "main: b = " << b << endl;

cout << "main: c = " << c << endl;

 

return 0;

}

 

 

           

Questions:

a.     Before you run the program, what do you predict as the output?

 

b.     Now run the actual program.  What was the observed output?

 

c.     If your predictions did not precisely match the observed output, explain the cause of the differences.