Thursday, May 29, 2014

function/subroutine in f90 (100th blog)

program aaa
   implicit none

   ! declaration : var + function
   integer i
   integer f1_ex    !!!!  must declare external function
   integer f2_ex    !!!!

   print *, f1_in()
   print *, f2_in()
   print *, f1_ex()
   print *, f2_ex()

   contains
      !====================
      ! internal function
      !====================
      ! way 1
      function f1_in() result (res)
         integer res
         res = 100
         return      !!! Not return res
      end function

      ! way 2 : function name as return variable
      integer function f2_in()
         f2_in = 200
         return
       !!FUNCTION must be present on the end-function-stmt of an internal or module function
      end function   

     !====================
     ! internal subroutine
     !====================
      subroutine s1_in()
         print *, 'this is internal sub'
      end subroutine




end



!====================
! external function
!====================
! way 1
function f1_ex() result(res)
   integer res
   res = 222
   return
end            !!! No function needed for external function

! way 2 : function name as return variable

integer function f2_ex()
   f2_ex = 333
   return
end

!====================
external subroutine  
!====================
subroutine s1_ex()
   print *, 'this is external sub'
end

No comments:

Post a Comment