Procedure and Recursion

In order to explain the involved ideas, let us consider the following simple examples of a procedure and a program that calls the procedure. In order to simplify the discussion, in the following, we assume that the inputs etc., are always of the required types only, and make other simplifying assumptions.

Example 1
Procedure sum-square (a, b : integer) : integer;
{denotes the inputs a and b are integers and the output is also an integer}
S: integer;
{to store the required number}
begin
S ← a2 + b2
Return (S)
end;

Program Diagonal-Length
{the program finds lengths of diagonals of the sides of right-angled triangles whose
lengths are given as integers. The program terminates when the length of any side is not positive integer}
L1, L2: integer; {given side lengths}
D: real;
{to store diagonal length}
read (L1, L2)
while (L1 > 0 and L2 > 0) do
begin
D← square─root (sum-square (L1, L2))
write (‘For sides of given lengths’, L1, L2, ‘the required diagonal length is’ D);
read (L1, L2);

end.

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 !!