ABSTRACT

A function is used to calculate a result whose value will generally depend on the values of “arguments” given each time that the function is invoked. A function is like a mini-program and has an overall structure deliberately similar to a main program: it starts with a title statement, followed by non-executable declaration statements, and then the series of executable statements finishing finally with a version of the END statement. An example is

FUNCTION Cot(x) REAL :: Cot

REAL, INTENT(IN) :: x REAL :: s, c

IF (x==0.0) THEN WRITE (*,*) "Error: cotangent function called with & &zero argument"

Cot = HUGE(x)

ELSE

s = SIN(x); c = COS(x); Cot = c/s END IF

END FUNCTION Cot

This piece of code would be provided alongside a main program. Whenever there is any reference to Cot in the main program (or for that matter anywhere else in the program as a whole, including other functions) this function will be entered and the cotangent will be calculated. When the END FUNCTION statement is reached, control returns back to wherever the reference to Cot was made.