PROG
read in the dimension
compute the area of the square side = dimension
compute the area of the circle radius=dimension
compute the area of the square side = 2*dimension
compute the area of the circle radius=2*dimension
PED
to learn procedures, no parameters, no local variables
CONCEPTS
between var and begin of main program
procedure proc-name;
begin
end;
write "proc-name;" to call it
We can use procedures for three purposes.
a. When we have common code that we don't want to copy over and over again. b. As an organizational tool to improve the readability of large programs c. to set up subroutine libraries.By the time, we are up to this unit, we have already used procedures in prepackaged libraries. read and write are builtin. We also could bring in the graphics library or the crt library as we saw earlier. In addition, there are a wide variety of mathematical functions such as sqr, sqrt, sin, cos, etc.
In this unit, we will learn how to write simple procedures that don't take arguments. In sqrt(x), x would be the argument.
Procedures must be included after the last variable defined in the "var" section and before the "begin" that starts the "main program." All our executable statements, such as read, write, assignment statements are found in the "main program."
The syntax to define a procedure is very simple. ("procedure" is a synonym for "subroutine.")
We begin it by writing the keyword procedure followed by an identifier. The identifier is the name of the procedure and I refer to it in the transparency by "proc-name." Then there is a begin-end pair. Between "begin" and "end" are the statements to be executed by the procedure.
In the main program, we have several "calls" to the procedure. When we call a procedure, the program will execute the statements inside the procedure. When we hit the end of the procedure, we will go to the next statement after the call. The computer will keep track of where we were called from when we start executing the procedure. That way, when we hit the "end" at the end of the procedure, we go back to the place we called it from. Note that we often have several calls to the procedure in a main program. Every time we call the procedure, the program returns to the statement just after the call in question.
How do we "call" a procedure. This is even simpler. We simply write the procedure name and a semicolon. The procedure name is the name after the keyword "procedure" when we defined it.
Currently, any variable that is defined in the var section of the main program--where we usually define them--is known to the procedure. The procedure can use their values and if the procedure changes them, the procedure changes the value, the main program will find them changed. We will learn how to make variables "local" to a procedure in an upcoming unit. We will also learn how to pass arguemnts to procedures.
Our example program does the following steps:
1. read in a number--call it dimension. 2. Compute the area of a square whose side has a length equal to dimension. 3. Compute the area of a circle whose radius has a length equal to dimension. 4. Compute the area of a square whose side has a length equal to twice dimension. 5. Compute the area of a circle whose radius has a length equal to twice dimension.In case we decide later to change the formula for computing the area of a square or an area of a circle, we make each of these computations a procedure. (Of course, these computations are very simple. We really don't need to make them procedures. But you could easily envision some complicated messy formula that you wouldn't want to have to type in more than once.)
Thus each of the items 2 through 5 correspond to a procedure call.
The procedure to compute the area of a square is called
"AreaSquare" It is defined on lines 3 to 6. Note the "procedure"
follwoed by AreaSquare on line 3. AreaSquare is the "rocname" The
body of the procedure, that is the statements executed when the
procedure is called, is found on lines 5.
Note that the procedure expects the side of the square in the
variable "side."
When the procedure returns, the main program can find the "answer"
in the variable "Area."
Likewise, the procedure to compute the area of a circle is in lines 7 to 10. It expects the radius of the circle to be in the variable "r" Again, it leaves it answer in the variable Area. (Pi is a variable that the system defines to be "pi" or 3.14159 for us.
Lines 14 to 17 handle step 2. We set side = dimension. We call the Procedure to compute the area of a square inm line 15. Line 16 to prints outs the result, which it of course finds in the variable "Area."
Lines 18 to 21, computes the area of the appropriate circle. Note that line 18 sets r to the dimension. The call is in line 19. After Area Circle finishes computing the area for a circule whose radius = dimension, we return to the statement after AreaCircle, line 20.
Lines 23 to 26 comptues step 4, the area of a square whose side is double dimension. Note in line 23 we set side to 2*dimension. Again, we call AreaSquare in line 24 and this time, the procedure will return to line 25.
And finally, in lines 27 to 30, we compute the area of a circle whose radius=2*dimension. Note that after AreaCircle is called in lines 28, we go back to line 29.
You may have noticed that it is quite a pain, to always have to
leave the values that a procedure will work on in a global
variable. In this case, we always put the radius in "r" for
AreaCircle and the side of a square in "side" for AreaSquare.
Parameters make this much easier to read and write--we will see
those in Unit 6.2