











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












(B) Subroutines:
Examples:
Our subroutine will be called from inside the main program via the CALL statement, as in:
CALL TWIG(x,s,c,t)
input output
We can write arguments in any order we like, e.g., instead
CALL TWIG(s,c,t,x)
And then the subroutine itself (the code) begins with the same name and argument list – in the same order, as in:
SUBROUTINE TWIG(x,s,c,t)
This equates:
x ↔ xin s ↔ ssin c ↔ ccos t ↔ ttan
Values are shared between names as above.
The subroutine code itself – after the 1 st^ line – is a stand-alone piece of code.
Example from above:
SUBROUTINE TWIG(XIN, SSIN, CCOS, TTAN) ! IMPLICIT NONE REAL :: XIN, SSIN, CCOS, TTAN ! SSIN=SIN(XIN) CCOS=COS(XIN) TTAN=SSIN/CCOS ! END SUBROUTINE TWIG
Example 2
To add two arrays, each 20 x 40 in size, we might have:
Inside the main program…
REAL, DIMENSION(20,40) :: A, B, C INTEGER :: M, N …input/specify values of M, N, A, and B …more code… …at this point, the value of C is unknown… CALL ARRSUM(A, B, M, N, C) …this returns the value of C = A + B …more code…
How do we insert subroutine code into main code?
Same choices as with functions:
a) At end b) Internally using CONTAINS statement