Set5 - Handout Five 1 Internal Functions Subroutines The main program unit can accommodate(`CONTAINS two types of procedures(often called

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Handout Five October 28, 2011 1 The main program unit can accommodate (‘ CONTAINS ’) two types of procedures (often called sub- programs). These can be used to break up a large program and structure it into a more ‘readable’ and efficient. Like the fortran intrinsic functions you have seen they can be repeatedly called to execute a series of statements that describe an often used task. This saves typing a sequential set of statements repeatedly in your code. Internal procedures come in two different forms a ‘ SUBROUTINE ’ or a ‘ FUNCTION ’. They are positioned inside the main program unit after the program execution statements and after a CONTAINS ’ statement. The following now details the syntax of the main program unit. PROGRAM <name> < Specification section > < Executable Statements > CONTAINS !*** Internal Procedures Follow < Internal Procedure One > < Internal Procedure TWO > : : : < Internal Procedure n > END PROGRAM <name> 1.1 Subroutines A subroutine, just like the main program, must have a ‘name’ and this name is used to ‘call’ the subroutine during program execution. Inside the subroutine you can declare data types just as you have been doing in the specification section of the main program unit. You can pass information from the main program 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
to the subroutine through an ‘argument list’ to the subroutine. Exercise One : An example of a simple subroutine without any arguments is given below. In ‘ handout5/exercise1 ’ directory type in and compile the following code. PROGRAM internal1 ! **** Example of a Program with an internal subroutine IMPLICIT NONE REAL :: a,b,c CALL calc PRINT ’("Answer = ",f10.4)’,c CONTAINS SUBROUTINE calc PRINT*,"Enter number one" READ*,a PRINT*,"Enter number two" READ*,b c=SQRT(a**2+b**2) END SUBROUTINE calc END PROGRAM internal1 The program has one procedure in the form of a subroutine called ‘calc’. The main program unit declares three ‘ REAL ’ data types and then calls the subroutine ‘ calc ’ using the Fortran keyword ‘ CALL ’. Then program execution moves into the subroutine ‘ calc ’ which prompts the user to input the two variables and then calculates a 2 + b 2 . Execution then moves back into the main program (after the ‘ CALL calc ’ ) and continues on the next line where the result of the calculation is written to the screen. If we wanted to we could call ‘ calc ’ many times from the main program execution section. This would of course be useful if we needed to calculate ‘ SQRT(a**2+b**2) ’ several times. 1.2 Argument lists The example code above ‘ internal1 ’ can also be written so it uses an argument list to pass the variables through to the subroutine calc. This is done by appending a pair of brackets to the subroutine name in the ‘ CALL ’ statement, and the arguments placed inside the brackets as a comma separated list. The arguments are received into the procedure declaration in the same way using parenthesis. The
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/13/2012 for the course MATH 5806 taught by Professor Brooks during the Fall '10 term at Minnesota.

Page1 / 11

Set5 - Handout Five 1 Internal Functions Subroutines The main program unit can accommodate(`CONTAINS two types of procedures(often called

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online