University of Pittsburgh
Summer 2001
CS401:  Introduction to Computer Science
Lab 3:  Procedural Programming
INTRODUCTION

What are procedures?

Programming languages use procedures to enclose a group of instructions. A well-written procedure is typically short in length and accomplishes a narrowly defined sub-task of the overall problem. The instructions in the procedure operate on data. This data may be local to the procedure or may be passed to the procedure by the calling program. The procedure, when complete, may pass data back to the caller.
 

Why do we use procedures?

When building complex systems we often have the following goals in mind:
    • The system should be reliable
    • The system should employ the principles of encapsulation and abstraction
    • The system should be modular
    • Components of the system should be organized in a meaningful way
    • The system should be easy to use and understand
    • The system should be maintainable
    • The system should be cost-effective
    • The system should be built from reusable components
The careful use of procedures in a program can help us attain, directly or indirectly, many of these goals. The use of procedures can promote understanding of the system by breaking the program into modules that act as abstractions. Some procedures may be re-used in future projects or taken from old projects or taken from an available library. The overall program size may be reduced because one procedure may be called several times. 
 

Void

You've seen various types now in your study of C++, like int and float, etc.  Void is not really a type (you can't go declare a variable of type void), but it can be used when you need to tell C++ that no type is involved here.  For example, think of the Math library function sqrt().  It returns a double, so we say that double is the return type.  What if a function doesn't need to return anything?  What if it's job is to just do a task and that's the end of it?  Well, that's what void is for.  By the way, if you're having trouble thinking of such a function (one that produces no answer), you'll find an example in Figure 3.1.
 
OBJECTIVES

In this lab, you will learn about C++ functions, how to define them and how to use them.  You will not learn all there is to learn about parameters, but you will see the two most imporant kinds of parameters (value and reference) and see what effect they have.
 
WHAT TO DO

Some example procedures in C++

As you've probably guessed, in C++, procedures are called functions. These functions are made up of a series of C++ statements and may or may not take parameters and may or may not return values. Figure 3.1 takes no parameters and returns no values. Figure 3.2 takes an integer value parameter n and returns an integer via the return statement. Figure 3.3 takes an integer value parameter and produces three results via reference parameters
 
 

// This function has no parameters and produces no return value.
// It displays a line of asterisks on the screen.

void manystars( ) {
  for (int h = 0; h <= 79; h++) 
    cout << "*";
}
 

Figure 3.1

manystars() just prints a bunch of asterisks on the screen.  There is no value (or "answer") when it is done, it just finishes and that's it!  So, we use void as the return "type" to make this clear.
 


// This function computes the sum from 1 to n
// It takes an integer value parameter and returns an integer result

int sum(int n) {
  int sumval = 0;

  for(int j = 1; j <= n; j++) 
     sumval += j;

  return sumval;
}
 

Figure 3.2

 
 

// This function takes a number in total seconds and converts
// it to hours, minutes and seconds. Note the use of reference 
// parameters.

void convertto_hms(int& hours, int& minutes, int& seconds,
                     long int totalseconds) {
   hours = totalseconds / 3600;
   totalseconds = totalseconds % 3600;
   minutes = totalseconds / 60;
   totalseconds = totalseconds % 60;
   seconds = totalseconds;
}
 

Figure 3.3

Don't worry about understanding all the details (although the code in the bodies of these functions should ), this lab will focus on how these functions are used, rather than how they actually work.  You should, however, analyze them in terms of the three big issues involved with any function:  

  1. the name of the function
  2. the input (the number of inputs and the types of each)
  3. the return type

How do I organize programs that use functions?

There are two ways that one can organize a program that uses functions. The functions can be placed in the same file as the main() routine or they can be placed in separate files from the main() routine. In either case, the compiler should see function prototypes before it sees the call on the function.

A function prototype is a description of the function rather than the function itself (function definition). For example, the functions above can be described with the following prototypes:
 

void manystars();
int sum(int);
void convertto_hms(int&, int&, int&, long int);
Notice that there are no parameter names in the prototypes.  Although you are allowed to put them in, it is generally not necessary (it is necessary in the function definition, however, as in the figures).  The prototype is terminated with a semi-colon and describes the number and type of any input to or output from the function.
 

Placing the functions in the same file as main()

A C++ program may be built from functions using the format shown in Figure 3.4 below. Note that the prototypes (or declarations, or headers) all appear prior to the main routine.
 
 
#include standard header files

function1 prototype; // All functions are found in this file
function2 prototype;
:
functionn prototype;

main function definition
  call function2
  call function1
  call functionk
end of main function

function1 definition

function2 definition

:

functionn defintion
 

Figure 3.4

So, here's what you need to do.  Following the format of Figure 3.4, start a new Borland project (name it something appropriate)  and write a program with the usual headers (#include lines), but place the prototypes (the three listed earlier) next, then an empty main() section (with nothing between the braces yet), then enter the three function definitions from figures 3.1 through 3.3, one after another, all after the ending right brace of main.  You are following the layout in figure 3.4.

Now enter the following line in the main block:
 

manystars();


In addition to this, go ahead and #include <conio> at the top of your file and put a call to getch() at the bottom of your main.  Other than this (and the return 0; line), this should be the only line in your main block.  

Run the program and enjoy the stars!  Do it again, but add 3 or 4 more calls to manystars().  Check out all the stars, it's like you just caught a swift uppercut!

Go ahead and remove all but one call to manystars(), and then add the following code:
 

int quickSum; 
quickSum = sum(10);
cout << "the sum of 1 to 10 is " << quickSum << endl;
cout << "the sum of 1 to 20 is " << sum(20) << endl;
Run the program, you should see the row of stars, then the output from the two print statements here.

Finally, add the following code segment to main():
 

int hr, min, sec;
convertto_hms(hr, min, sec, 8800);
cout << "hours = " << hr << endl;
cout << "minutes = " << min << endl;
cout << "seconds = " << sec << endl;
Run the program.

Finally, print out your source code (to turn in with the worksheet).  Make sure your name is on the top in a comment.

 

LOOK IT UP

Go into Borland Help and look up "Reference Argument".  Read the first section (everything above "Implementation 1").  This describes the difference between value and reference parameters.