13.07.2015 Views

Fortran 90/95 Programming Manual

Fortran 90/95 Programming Manual

Fortran 90/95 Programming Manual

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.

<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>Programming</strong> <strong>Manual</strong>ProceduresA subprogram or procedure is a computation that can be “called” (invoked) from theprogram. Procedures generally perform a well-defined task. They can be eitherfunctions or subroutines. Information (data) is passed between the main program andprocedures via arguments. (Another way of passing information is via modules, seeChapter 6.) A function returns a single quantity (of any type, including array), andshould, in principle, not modify any of its arguments. (In the stricter F language, afunction is simply not allowed to modify its arguments). The quantity that is returned isthe function value (having the name of the function). We have already seen one type offunctions in Chapter 2, namely built-in or intrinsic functions, which are part of the<strong>Fortran</strong> <strong>90</strong> language (such as cos or sqrt).An example of a function:function circle_area (r)! this function computes the area of a circle with radius rimplicit none! function resultreal :: circle_area! dummy argumentsreal :: r! local variablesreal :: pipi = 4 * atan (1.0)circle_area = pi * r ** 2end function circle_areaThe structure of a procedure closely resembles that of the main program. Note also theuse of implicit none. Even if you have specified implicit none in the main program, youneed to specify it again in the procedure.The r in the function circle_area is a so-called dummy argument. Dummy arguments arereplaced by actual arguments when the procedure is called during execution of theprogram. Note that the function has a “dummy arguments” block and a “local variables”block, separated by comments. While this is not required, it makes the program clearer.The function can be used in a statement like:a = circle_area (2.0)This causes the variable a to be assigned the value 4π.This is, by the way, not a very efficient way to calculate the area of a circle, as π isrecalculated each time this function is called. So if the function needs to be called manytimes, it will be better to obtain π differently, for example by declaring:real, parameter :: pi = 3.14159265424

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

Saved successfully!

Ooh no, something went wrong!