Defining Your Own Fortran Functions

Fortran has many intrinsic functions like sin(), sqrt(), etc. for common mathematical operations, but no language can provide a function for everything a programmer might want to compute.

Hence, Fortran allows us to define our own functions and even make them available for use in other programs.

A Fortran function definition looks almost exactly like the main program. There are only a few subtle differences. The general form of a function definition is as follows:

! Define function
function function_name(argument variable list)
    data type :: function_name
    
    implicit none
    
    ! Define arguments
    data type, intent(in|out|inout) :: arg1, arg2, ...
    
    ! Define local variables
    data type :: var1, var2, ...
    
    ! Function body
    
    ! Return value by assigning to function name
    ! Every function MUST do this before returning to the caller!
    function_name = some-expression

end function function_name
        

Below is a complete program with a user-defined function. In addition to the function definition, the program contains a subprogram interface for the function, which is explained in the section called “Type Matching”.

!-----------------------------------------------------------------------
!   Program description:
!       Program to print a table of integer powers
!-----------------------------------------------------------------------

!-----------------------------------------------------------------------
!   Modification history:
!   Date        Name        Modification
!   2011-03-23  Jason Bacon Begin
!-----------------------------------------------------------------------

! Subprogram interfaces
module subprograms
    interface
        function power(base, exponent)
            real(8) :: power
            real(8) :: base
            integer :: exponent
        end function
    end interface
end module

! Main program body
program function_example
    use subprograms
    
    ! Disable implicit declarations (i-n rule)
    implicit none
    
    ! Variable definitions
    integer :: exponent
    
    ! Statements
    print *, 'Powers of 2'
    do exponent = 0, 10
        ! Will not work with 2.0 instead of 2.0d0
        print *, '2 ^ ', exponent, ' = ', power(2.0d0, exponent)
    enddo

    print *, ''
    print *, 'Powers of 3'
    do exponent = 0, 10
        ! Will not work with 3.0 instead of 3.0d0
        print *, '3 ^ ', exponent, ' = ', power(3.0d0, exponent)
    enddo
end program

!-----------------------------------------------------------------------
!   Description:
!       Compute base ** exponent for any non-negative integer exponent
!
!   Arguments:
!       base:       The real base of base ** exponent
!       exponment:  The non-negative integer exponent
!
!   Returns:
!       base ** exponent
!-----------------------------------------------------------------------

!-----------------------------------------------------------------------
!   Modification history:
!   Date        Name        Modification
!   2011-03-23  Jason Bacon Begin
!-----------------------------------------------------------------------

function power(base, exponent)
    ! Disable implicit declarations (i-n rule)
    implicit none
    
    ! Function type
    ! Factorials grow beyond the range of a 32-bit integer very quickly
    ! so we use a data type with greater range
    real(8) :: power
    
    ! Dummy variables
    real(8), intent(in) :: base
    integer, intent(in) :: exponent
    
    ! Local variables
    integer :: x
 
    ! Compute n!
    power = 1.0
    do x = 1, exponent
        power = power * base
    enddo
end function