Basic Functions and Subroutines A computer program which is complex and large, can be broken up into many subprograms. There is only one “main” program and the other subprograms are called subroutines or functions. The subprograms can be larger than the main program. |
Functions are designed so they can appear in an algebraic expression, for example, the expression q=abs(cos(x)-sin(y))/sqrt(1.0+atan(x+y)) |
There are 5 different intrinsic functions: abs, sin, cos, sqrt and atan. Intrinsic functions are those which are provided by the Fortran library. The program can also be written with specific functions which are not provided by the standard Fortran library. |
Let us consider add the values of two variables, A and B, to produce C. The Fortran code can be used to add. |
Let C = A + B Using a subroutine ADD, the call in the main program is: REAL A, B, C . . . CALL ADD (A, B, C) |
The subroutine would look like: |
SUBROUTINE ADD (X, Y, Z) |
REAL X, Y, Z |
Z = X + Y |
RETURN |
END |
Here C in the main program takes on the value from the Z variable in the subroutine, X in the subroutine takes on the value from the main program for variable C. If A is real, X must be real. |
Functions and subroutines are procedures and these have four types: |
1) Statement functions. |
2) Intrinsic functions or Built-in Function. |
3) External functions. |
4) Subroutines. |
Intrinsic functions, statement functions and external functions are collectively known as functions. Intrinsic functions are those provided by the Fortran library. |
External functions and subroutines are collectively known as external procedures. |
Fortran 90 have approx 100 intrinsic functions and few of extrinsic subroutine. |