Procedure and Recursion

In order to explain, how diagonal length of a right-angled triangle is computed by the program Diagonal-Length using the procedure sum-square, let us consider the side lengths being given as 4 and 5.

First Step: In program Diagonal-Length through the statement read (L1, L2), we read L1 as 4 and L2 as 5. As L1 > 0 and L2 > 0. Therefore, the program enters the
while-loop. Next the program, in order to compute the value of the diagonal calls the procedure sum-square by associating with a the value of L1 as 4 and with b the value of L2 as 5. After these associations, the procedure sum-square takes control of the computations. The procedure computes S as 41 = 16 + 25. The procedure returns 41 to the program. At this point, the program again takes control of further execution. The program uses the value 41 in place of sum-square (L1, L2). The program calls the procedure square-root, which is supposed to be built in the computer system, which temporarily takes control of execution. The procedure square-root returns value √41 and also returns control of execution to the program Diagonal-Length which in turn assigns this value to D and prints the statement:

For sides of given lengths 4 and 5, the required diagonal length is √41 .
The program under while-loop again expects values of L1 and L2 from the user. If the values supplied by the user are positive integers, whole process is repeated after entering the while-loop. However, if either L1 ≤ 0 (say ─ 34) or L2 ≤ 0, then whileloop is not entered and the program terminates.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!