26.09.2013 Views

Chapter 11. More Fortran Elements: Functions and Subroutines 11.1 ...

Chapter 11. More Fortran Elements: Functions and Subroutines 11.1 ...

Chapter 11. More Fortran Elements: Functions and Subroutines 11.1 ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

subroutine name(arg1, arg2, …) start of subroutine<br />

implicit none<br />

variable declarations<br />

real arg1, arg2, x, y<br />

integer i, j, k<br />

.<br />

.<br />

.<br />

calculations are carried<br />

out here<br />

.<br />

.<br />

.<br />

return<br />

end<br />

main body of subroutine<br />

end of subroutine<br />

Figure <strong>11.</strong>2. Layout of a typical <strong>Fortran</strong> subroutine.<br />

The SUBROUTINE statement is placed at the beginning of the subroutine <strong>and</strong> identifies<br />

the subroutine name. The arguments passed to <strong>and</strong> from the subroutine are enclosed in parentheses<br />

after the subroutine name. Any number of arguments may be passed back <strong>and</strong> forth <strong>and</strong> the<br />

arguments may correspond to any variable type <strong>and</strong> also may be arrays.<br />

The Variable Declaration section of the subroutine has the same format as it does in a<br />

regular <strong>Fortran</strong> program. Note that the arguments passed between the subroutine <strong>and</strong> the main<br />

program must be declared in the Variable Declaration section.<br />

The Main Body of the subroutine has the same format as in a regular <strong>Fortran</strong> program.<br />

The main computational tasks of the subroutine are carried out in this section.<br />

The End of the Subroutine is indicated by the statement RETURN, which passes<br />

execution back to the main program, <strong>and</strong> END, which signifies the end of the subroutine.<br />

<strong>11.</strong>3.2 Using a Subroutine in a <strong>Fortran</strong> Program<br />

A subroutine is employed in a <strong>Fortran</strong> program in a slightly different way than an external<br />

function. Because the subroutine may pass back to the main program multiple numerical results,<br />

the subroutine is not included as part of an assignment statement. Rather, a CALL statement is<br />

employed. The format of a typical CALL statement is shown below.<br />

CALL NAME(ARG1, ARG2, …)<br />

In the CALL statement, NAME refers to the name of the subroutine. Note that the subroutine name<br />

should not be the same as any other variable name <strong>and</strong> unlike an external function, it should not be<br />

declared as a real or integer variable. This is because a subroutine may pass back to the main<br />

program the values of many variables, both real <strong>and</strong> integer, while a function only returns a single<br />

value.<br />

71

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!