CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program Subprograms make it readable and simple by dividing it into smaller programs. set of operations is to be repeated Subprograms are used to avoid repetition.
FORTRAN language has two types of subprograms: FUNCTIONS: return single value. SUBROUTINES: return more than one value. Functions: There are two types of functions: INTRINSIC functions example: ConstantSIN (10.0) VariableSIN (theta) ExpressionSIN (2.0*alfa + theta/3.0) Other functionSIN (ABS (a-b))
USER-DEFINED functions Example: PROGRAM STATMENT_FUNCTION IMPLICIT NONE INTEGER,DIMENSION(3,4)::RENT INTEGER :: SET,I,J,L,K SET(K,L) = K +L/3 DO I=1,3 DO J=1,4 RENT(I,J)=SET(I,J) END DO PRINT*,'ELEMENTS OF ARRAY RENT ARE:' DO I=1,3 PRINT*,(RENT(I,J),J=1,4) END DO STOP END PROGRAM STATMENT_FUNCTION
External Function Subprogram External function subprogram can be made accessible to a program unit by attaching it after the END statement of the main program. EXAMPLE Write a main program to read three values and prints their average. The average values should be calculated using an external function subprogram. PROGRAM Test REAL :: Test 1, Test 2, Test 3, Ave READ *, Test 1, Test 2, Test 3 PRINT 5, Ave (Test 1, Test 2, Test 3) 5FORMAT (1X, ‘Average=’, F5.2) STOP END PROGRAM Test REAL FUNCTION Ave (X, Y, Z) REAL :: X, Y, Z Ave=(X+Y+Z)/3.0 RETURN END FUNCTION Ave
EXAMPLE : Follow the execution of the following program and show its output? PROGRAM FUNCTION_SUBPROGR AMS IMPLICIT NONE REAL :: A,B,C,TIME,ADD A = 1.0 B = 2.0 C = 3.0 TIME = 2.0*ADD(A,B,C) PRINT 10, TIME 10 FORMAT (1X, 'TIME=', F6.2) STOP END PROGRAM FUNCTION_SUBPROG RAMS ! ! FUNCTION SUBPROGRAM REAL FUNCTION ADD(X,Y,Z) REAL :: X,Y,Z Y = X +Y Z = Y + Z X = Z + X ADD = X + Y + Z RETURN END FUNCTION AD
Subroutine Subprogram Subroutines are modulus written to perform operations that cannot be performed by a function. For example, if several values need to be returned from a module, a sub-routine is used. A subroutine is also used for operations that do not compute values, such as reading the values in a data file. Subroutines may be either internal or external. External Subroutines Subprogram External subroutines can be made accessible to a program unit by attaching it after the END statement of the main program.
Subroutines Rules 1.A subroutine does not represent a value; thus, its name should be chosen for documentation purposes and not to specify a real or integer value. 2.A subroutine is referenced with an executable statement whose general form is CALL subroutine name (argument list) 3.The first line in a subroutine identifies it and includes the subroutine name and argument list, as follows: SUBROUTINE name (argument list) 4.A subroutine uses the argument list for subroutine inputs and values returned to the calling program. arguments in CALL statement actual arguments. arguments in SUBROUTINE dummy arguments. The arguments in the CALL statement must match in type, number, and order those used in the subroutine definition.
5.A subroutine may return one value, many values, or no value. Similarly, a subroutine may have one input value, many input values, or no input value. 6.Arguments are the link between the main program and the subroutine. Thus, the choice of subroutine statement numbers and variable names is independent of those in the main program. Variables used in subroutine and not subroutine arguments local variables their values are inaccessible from main program. 7. The subroutine, like the function, requires a RETURN statement to return control to main program.It also requires an END statement. 8.A subroutine may reference other functions or call other subroutines, but it cannot call itself.
EXAMPLE: Write a Fortran program which uses a subroutine program to calculate the average, the minimum value and the maximum value of an array X with number of elements N represents the scores of students grade. PROGRAM Scores INTEGER :: N, I REAL :: Tests (100), Ave,Min,Max READ*, N READ*, (Tests(I), I=I,N) CALL Stat (Tests, N, Ave, Min, Max) Print 5, Ave, Min, Max 5Format (1X, 3F10.3) END PROGRAM Scores SUBROUTINE Stat (X, M, Xave, Xmin, Xmax) INTEGER :: M, I REAL :: X(M), Xave, Xmin, Xmax, Sum Sum=X(1) Xmax=X(1) Xmin=X(1) DO I=2,M Sum=Sum+X(I) IF (X(I) < Xmin) Xmin=X(I) IF (X(I) > Xmax) Xmax=X(I) END DO Xave=Sum/M RETURN END SUBROUTINE Stat
EXAMPLE Follow the execution of the following program and show its output? PROGRAM SUBROUTINE_EXAMPLE IMPLICIT NONE INTEGER,DIMENSION (10):: K INTEGER::I DO I=1,10 K(I)=I END DO CALL MODIFY (K,10) PRINT*,"NEW VALUES OF K ARE:" PRINT 15, (K(I),I=1,10) 15 FORMAT(1X,5I5) END PROGRAM SUBROUTINE_EXAMPLE SUBROUTINE MODIFY(L,N) INTEGER :: I,N INTEGER, DIMENSION (N):: L DO I=1,N L(I)=MOD(L(I),4) END DO RETURN END SUBROUTINE MODIFY