Summer 2001 CS401: Introduction to Computer Science Lab 3: Procedural Programming |
||||||||
|
||||||||
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:
VoidYou'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. |
||||||||
|
||||||||
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. |
||||||||
|
||||||||
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.
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.
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:
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();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.
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();
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;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;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.
|
||||||||
|
||||||||
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. |