ICS 101 - Computer programming

Lab # 7

(Subroutines)

 

Objectives

 

 

A subroutine may return zero, one or many results. (Remember that a function can return only one result). Unlike function, a subroutine name can not be used as a variable to hold results.

 

Syntax

 

 

 

 

 

 

 

 

 

 

Example

 

 

        

 

 

     Following is the output of the program.

 

 

          

 

 

Exercise 1

 

Write a program which reads the length and width of a rectangle and prints the area and perimeter of that rectangle. The area and perimeter must be computed by a subroutine  AREAPR called by the program. Use any positive numbers as data.

 

 

Exercise 2

                                                                                                                  

Write a program which reads the surface area s of  a  right  circular cylinder. The program then calls a subroutine that computes the radius r and the volume v of that cylinder. And pass them back to the main program. The main program then prints these two values.

 

Use the data: 155.0

Your output should be in the form:

 

RADIUS  =  6.536668  CM

SURFACE AREA  =  155.000000  CM SQUARED

VOLUME  =  506.591800  CM CUBED

 

NOTE:                 

                         ___                       ___

            v = pr3 3.0,           s = 2pr2 3.0,         p  = 3.14159

 

 

 

Exercise 3

 

Write a subroutine that converts a number of seconds into hours, minutes, and seconds. For example, 3724 seconds is 1 hour, 2 minutes, and 4 seconds. Write a main program to test the subroutine.

 

 

 

Exercise 4

 

Given the number of hours worked by an employee in a week and the payment per hour, write a subroutine that returns the regular payment, over-time payment, and the total payment. The over-time payment is calculated as follows: every hour after 40  is considered as 1.6 hours. Write a main program to test the subroutine.