6.1 Solutions

  1. A procedure definition describes what the procedure will do. As an example in question 3, lines 1-3 are part of the definition of the procedure chorus. But no code gets executed until you call the procedure. A procedure call is when you use the name of the procedure to cause the code in the definition to be executed.
  2. You call a procedure by using its name (as an example in question 3 lines 5 and 7 are procedure calls)
  3. 4 5 1 2 3 6 7 1 2 3
    1.  
      procedure printTable
         const LARGEST := 10
         for i : 1 .. LARGEST
            put i, " ", i*i
         end for
      end printTable 
    2. Prints a table of the squares of the numbers from 1 to 10.
  4.  
    % part a
    procedure printRectangle
       const SYMBOL := '*'
       const HEIGHT := 3
       const WIDTH := 8
       
       for row : 1 .. HEIGHT
          for col : 1 .. WIDTH
             put SYMBOL ..
          end for
          put ""
       end for
    end printRectangle
    
    % part b
    printRectangle
    put "Now a second call"
    printRectangle
    put "One last time"
    printRectangle 
      1.  
        % include the procedure definitions from the question
        solid
        solid 
      2.  
        % include the procedure definitions from the question
        solid
        for i : 1 .. 6
           hollow
        end for
        solid 
    1.  
      var howMany : int
      
      procedure solid
         for i : 1 .. 20
            put "*" ..
         end for
         put ""
      end solid
      
      procedure hollow
         for i : 1 .. 20
            if i = 1 or i = 20 then
               put "*" ..
            else
               put " " ..
            end if
         end for
         put ""
      end hollow 
      
      put "How many rows do you want?"
      get howMany
      
      if howMany < 2 then
         put "Sorry that number is too small"
      else
         solid
         for i : 1 .. howMany - 2
            hollow
         end for
         solid
      end if