- You can cause a side effect if you change the value of a global variable in a procedure. It might be used for some other purpose some where else in the program. Ideally calling a procedure should not effect any other part of the program
- The procedure will be easier to reuse in another program if it doesn't use global variables
- By using parameters instead of global variables the procedure is much more flexible.
- (a) There are only 2 arguments. There should be 3
(c) The second argument should be an int, not a real
(d) The third argument should be a boolean, not a string
(e) All the arguments are the wrong types
-
% 4a) Why might length be a bad name for the parameter?
procedure putArea(length, width : real)
put "The area of the rectangle is ", round(length * width)
end putArea
% 4b)
var l, w : real
put "Enter the length"
get l
put "Enter the width"
get w
putArea(l, w)
procedure putRow(c : char, n : int)
for i : 1 .. n
put c..
end for
end putRow
const WIDTH := 80
procedure putRow(c : char, n : int)
for i : 1 .. n
put c..
end for
end putRow
procedure putTriangle(rows : int, symbol : char)
for i : 1 .. rows
put " ":WIDTH div 2 - i..
putRow(symbol, 1 + 2*(i - 1))
put ""
end for
end putTriangle
put "A small triangle of stars"
putTriangle(3, '*')
put ""
put "A large triangle of plus signs"
putTriangle(10, '+')
put ""
put "A very small triangle of dots"
putTriangle(1, '.')
put ""
procedure range (list: array 1 .. * of int)
var least, greatest : int
least := list(1)
greatest := list(1)
for i : 1 .. upper(list)
if list(i) < least then
least := list(i)
end if
if list(i) > greatest then
greatest := list(i)
end if
end for
put "The least value is ", least, " and the greatest is ", greatest
end range