Lahey/Fujitsu Fortran



A function is a procedure that produces a single scalar or array result. It is used in an expression in the same way a variable is. For example, in the following program,

 program main
   implicit none
   interface ! an explicit interface is provided
     function square(x)
       implicit none
       real, intent(in) :: x
       real :: square
     end function square
   end interface
   real :: a, b=3.6, c=3.8, square
   a = 3.7 + b + square(c) + sin(4.7)
   print*, a
 end program main
 function square(x)
   implicit none
   real, intent(in) :: x
   real :: square
   square = x*x
 end function square

square(c) and sin(4.7) are function references.

Function reference:

function-name (actual-arg-list)

function-name is the name of the function

actual-arg-list is a list of actual arguments

A function can be defined as an internal or external function or as a statement function.

External Functions

External functions can be called from anywhere in the program.

External function definition:








function-stmt is a FUNCTION statement.

use-stmts is zero or more USE statements.

specification-part is zero or more specification statements.

execution part is zero or more executable statements.

internal-subprogram-part is

CONTAINS procedure-definitions

procedure-definitions is one or more procedure definitions.

end-function-stmt is

END [FUNCTION [function-name] ]

function-name is the name of the function.