Thursday, March 14, 2013

Subroutines, Procedures and Functions

Subroutines, Procedures and Functions

Subroutines, Procedures and Functions

ROUTINES AND SUBROUTINES

A routine is a sequence of instructions to carry out a particular task. A'routine is a general tenn for a relatively small self-contained set of instructions. It may refer to:

1 A complete short program; or

2 A part of a program; or

3 A set of instructions used by other programs.

A subroutine is a routine which is used by one or more programs. A subroutine is not itself a complete program. It has to be called by other programs.

A program calls the subroutine and the subroutine's task is carried out. Control then returns to the correct place in the calling program, which continues from where it left off.

Often a computer system has a library of subroutines available for programmers to use.

A library subroutine is a subroutine which is available to all users of a computer system as part of the software.

PARAMETERS AND LOCAL VARIABLES

A parameter or argument is a value passed between a program and a subroutine. Most languages (though not some versions of BASIC) allow a programmer to do either of the following:

1 Use one name for a parameter in a subroutine and another name for the same parameter when it appears in the calling program.

2 To have a variable in a subroutine and another. one in the calling program, both with the same name. This is made possible by having local variables.

A local variable is one whose name is restricted to one subroutine.

A global variable is one which has the same name throughout a program and all its routines.

Advantages of these arrangements are:

1 A subroutine can be used in more than one program without worrying

(a) Whether names in the subroutine and the program clash; or

(b) Whether the right names have been used for the parameters.

2 Subroutines from subroutine libraries can be used without a problem over names.

3 The same subroutine can be used to operate on two different variables in the same program.

PROCEDURES

Languages such as COMAL and Pascal (and some versions of BASIC) allow a type of subroutine called a procedure. Procedures are generally used in stepwise refinement of a program .

In COMAL

1 Procedures are executed using an EXEC statement.

2 A procedure is defined between a PROC and an END PROC.

3 Parameters are put in brackets after the name of the procedure.

4 All variables in a procedure are made local using CLOSED.

Example of variables and parameters in COMAL

The following COMAL routine produces an introductory pattern for a game of noughts and crosses. It simply prints a pattern made of alternate blocks of o's and x's. In fact it repeats the following a times down the page:

00000000000000000000

00000000000000000000

00000000000000000000

00000000000000000000

xxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxx

100 //I ROUTINE TO PRINT ALTERNATELY BLOCKS OF o'S AND x'S //

200 FOR I : = 1 TO 3

210 EXEC BLOCK("o")

220 EXEC BLOCK("x")

230 NEXT I

299 //

300 PROC BLOCK(CHAR$) CLOSED 310 FOR I : = 1 TO 4

320 EXEC ROW(CHAR$)

330 NEXT I

340 END PROC BLOCK 399 //

400 PROC ROW (CH$) CLOSED 410 FOR I : = 1 TO 20

420 PRINT CH$;

430 NEXT I

440 PRINT

450 END PROC

Notes:

1 Because the procedures BLOCK and ROW are both CLOSED the variables in them are local. In particular the changes of I in ROW and BLOCK do not affect the I in the main program.

2 The variable CHAR$ in BLOCK is a parameter which takes first the value "0" and then the value "x".

FUNCTIONS

In high level programming a function is a sequence of instructions used by a program to supply a value in an expression.

There are two main types of function:

1 Built-in functions (or standard functions). These are functions which are always available to users of a particular language.

2 Programmer-defined (or user-defined) functions. These are functions written by the programmer.

Examples in BASIC

1 Built-in functions.

(a) LEN

100 LET N1=LEN(A$)

LEN is a function giving the number of characters in a string. For example, if A$ is the string "HELLO", Nl will be assigned the value 5.

(b) INT

110 IF Num= INT(Num) THEN PRINT "Whole number"

INT gives the whole number below the value of the expression in the brackets. For instance LNT(2.6) is 2 and INT( -3.4) =-4

Num will only be equal to INT(Num) if Num is a whole number.

2 Programmer-defined functions.

The following function C would correct a value to the nearest whole number. It uses the built-in function INT mentioned above.

Definition of the function C: 100 DEF FNC(N)~INT(N+.5)

Use of the function C to correct the values of Hand W:

500 PRINT "Approximate height and weight are ": FNC(Hl, FNC(W)

Using functions and procedures in top-down programming

In structured languages it is usual to write first the main structure of a program. The details are then filled in by defining functions and procedures. In other words these structures make stepwise refinement easier while doing top-down programming .

Example of the use of procedures in COMAL

The following program is a COMAL version of the program discussed . It has been written top-down in three levels as suggested there:

1 The function of the program is expressed in line 100.

2 This function is refined into procedures in lines 110 to 130.

3 The procedures are written in detail in lines 200 to 430.

100 //PROGRAM : Input three numbers and print out their average.//

110 EXEC input_ Three..Numbers

120 EXEC Calculate., Total_And_Average

130 EXEC Print-Nvumbers-And- Their-Average

140 END

199 //

200 PROC input-Three-Numbers

210 PRINT "The Average of Three Numbers"

220 PRINT

230 INPUT "Please type your first number ": Num1

240 INPUT "Please type your second number ", Num2

250 INPUT "Please type your third number ": "Num3

260 END PROC Input-Three Numbers

299 //

300 PROC Calculate., Total-And-Average

310 Total: = Numl + Num2 + Num3

320 Average: = Total 3

330 END PROC Calculate- Total-And-Average

399 //

400 PROC Print-numbers-And-Average

410 PRINT "The Numbers are :- ";Num1,Num2,Num3

420 PRINT "Their average is :- ": Average

430 END PROC Print-Numbers-And-Average