FORTRAN programs become very lengthy while solving real engineering problems. To deal with such lengthy codes, the sequences of codes are identified that perform the same operation on different data. Then the user defines a subprogram that performs the above function. This subprogram is invoked within the main program whenever required. Subprograms are of two types: Function subprogram and Subroutine subprograms. These are sometimes also called Procedures.
Function Subprograms
Functions take a set of input arguments and return a value, just in a similar way as mathematical functions do.
name ([argument_list])
The argument list is a list of parameters with commas in between. However, the use of arguments is optional. Parentheses must be included even when arguments are not being placed.
Intrinsic Functions
These are built-in functions defined as a part of the language. Most of the intrinsic functions are generic, i.e., they return a value of the same type as of their arguments are. For example ABS(X) returns an integer value if X is an INTEGER and it returns a real value if X is REAL. Some other generic intirnsic functions are MAX, MIN, MOD. The function INT(R), where R is a real number gives an integer value, hence it is a non-generic function. Some more examples of non-generic functions are COS(R), LOG(R), EXP(R) etc.
External Functions
The programmer may create his/her own functions which are entirely independent of the program. Independence means that the user can place them either in the same source file or separate object files. An external function program unit begins with a FUNCTION statement and ends with an END statement. The type and arguments of the function statement are optional. Type of the function corresponds to the type of value it returns. It can be specified either implicitly or by type specification statement. The main program unit begins with a PROGRAM statement and ends with an END statement.
PROGRAM MAIN . . . . . . . . . . . . . . END TYPE FUNCTION FUN1 (arg1, arg2,....) . . . . . . . . . . . . . . END TYPE FUNCTION FUN2 (arg1, arg2,....) . . . . . . . . . . . . . . END
Function Reference
A function reference has the form
name (argument_list)
The argument list is a list of actual arguments and it must match the arguments used while defining the function.
FUNCTION FUN1(A,B,N) REAL A(100) ...................... RESULT=FUN1(A,B,10)
Subroutine subprograms
Subroutine is also a subprogram similar to function subprograms, except that it does not return a value through its name but throough its arguments. Thus a subroutine does not have a TYPE. A subroutine program begins with a SUBROUTINE statement and ends with an END statement.
SUBROUTINE name [(argument_list)]
A subroutine may not have a argument list. In such case it has the form:
SUBROUTINE name
A subroutine must necessarily include a RETURN statement. It is referenced using a CALL statement.
CALL name [(argument_list)]