CSC133 Introduction to CS Using C++
Lab 3

Part I

Introduction

In Lab 2, you wrote a program with three functions. The function heading printed a heading in the output. The function calcsize calculated and printed the number of bytes used for different types of variables. The function main called the other two functions, otherwise they would not have been executed.

The functions we wrote for Lab 2 do not communicate with the functions that call (invoke) them. For example, function main calls function calcsize but main doesn’t know what function calcsize did when it was executing. Suppose we wanted function calcsize to calculate the size of an integer and then have function main print the number of bytes calculated by function calcsize. How can function calcsize tell function main the number of bytes that it calculated?

One way functions can communicate is that the called function (function calcsize) can return one value to the calling function (function main). Then the calling function (function main) can print the value returned. The value returned actually replaces the call to the function.

Assignment

Follow the instructions below to modify functions main and calcsize so that they can communicate via a return value.

  1. Open your program from Lab 2 Part 2.
  2. Use Save As to save the program under a different name, such as lab3.cpp.
  3. Modify function main as follows:
    1. Change the calling statement to:
          cout << "The number of bytes occupied by an integer is " <<
                     calcsize();
    2. Change the prototype for calcsize to:
          int calcsize();
  4. Modify function calcsize as follows:
    1. Change the function header to specify a return type of int instead of void.
    2. Delete all the cout statements.
    3. In the body of the function, between the braces { and }, add the statement:
          return sizeof(int);
  5. Print your program and your output.

Part II

Introduction

In Lab 2 we learned how to write functions. In the first part of the lab, the function we wrote had a return type of void. This means that the function does not return a value to the calling function, so there was no communication between the calling function (main) and the called function. In the first part of this lab, the called function returned a value to the calling function (main). This function is known as a value returning function and we specify this by writing the data type of the value returned in the function heading and function prototype instead of the word void. Our functions are now communicating by having the called function return information to the calling function.

Sometimes we need the calling function (main) to give information to the called function. How can we do this? We do this through the parameter list. The parameter list consists of values included between the parenthesis in the function prototype, the function header, and the function call.

For example, suppose addAndPrint is a void returning function that is passed two integer values as parameters. In the call statement, you place the values you wish to pass:

   int main()
   {
      int x,y;
      addAndPrint(5,6);      //one possible call statement
      cin >> x >> y;
      addAndPrint(x,y);      //another possible call statement
   }

In the prototype statement, you place the data types of the values you will pass:

   void addAndPrint(int, int);

In the function header, you place the data type and identifier name for each parameter passed. Inside the function you may use the parameters listed in the function heading just as you use variables. Note that the first parameter in the function heading gets the value of the first parameter (argument) passed in the call statement, the second parameter in the function heading gets the value of the second parameter passed, etc. The parameters in the call statement are known as the actual parameters. The parameters in the function heading are known as the formal parameters.

   void addAndPrint(int a, int b)
   {
      cout << a + b << endl;
   }

If we put all of the above pieces together, the program looks like:

   #include <iostream.h>

   void addAndPrint(int, int);

   int main()
   {
      int x,y;
      addAndPrint(5,6);      //one possible call statement
      cin >> x >> y;
      addAndPrint(x,y);      //another possible call statement
   }

   void addAndPrint(int a, int b)
   {
      cout << a + b << endl;
   }

We can combine the parameters and the return in the same function. Suppose we want to change our addAndPrint function so that it adds the parameters and returns the sum. Then the calling function (main) will have to print the result. Remember that to use the return, we need to modify the return type of addAndPrint in the prototype and in the function header. We also need to change the call statement so that the returned value is printed in main. And lastly, we need to add a return statement in addAndPrint. Here is what the program would look like now:

   #include <iostream.h>

   int addNoPrint(int, int);

   int main()
   {
      int x,y;
      cout << addNoPrint(5,6) << endl;   //one possible call statement
      cin >> x >> y;
      cout << addNoPrint(x,y) << endl;   //another possible call statement
   }

   int addNoPrint(int a, int b)
   {
      return a + b;
   }

Assignment

Write a program which uses functions to compute some information about a car trip:



Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2005