13.07.2015 Views

Fortran 90/95 Programming Manual

Fortran 90/95 Programming Manual

Fortran 90/95 Programming Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Fortran</strong> <strong>90</strong>/<strong>95</strong> <strong>Programming</strong> <strong>Manual</strong>The result of a function can be given a different name than the function name by theresult option:function circle_area (r) result (area)! this function computes the area of a circle with radius rimplicit none! function resultreal :: area! dummy argumentsreal :: r! local variablesreal, parameter :: pi = 3.141592654area = pi * r ** 2end function circle_areaThe name specified after result (area in this case) must be different from the functionname (circle_area). Also note the type declaration for the function result (area). Thisfunction is used in the same way as before:a = circle_area (radius)The result option is in most cases optional, but it is required for recursive functions, i.e.,functions that call themselves (see paragraph on “recursive functions” below).An example of a subroutine:subroutine swap (x, y)implicit none! dummy argumentsreal :: x, y! local variablesreal :: bufferbuffer = xx = yy = bufferend subroutine swap! store value of x in bufferA subroutine is different from a function in several ways. Subroutines can modify theirarguments, and they do not return a single “result” as functions do. Functions return avalue that can be used directly in expressions, such as:a = circle_area (radius)A subroutine must be “call”ed, as in:call swap (x,y)25

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

Saved successfully!

Ooh no, something went wrong!