Private function in Fortran

前端 未结 4 1399
感情败类
感情败类 2021-02-07 11:59

How do I declare a private function in Fortran?

相关标签:
4条回答
  • 2021-02-07 12:32

    I've never written a line of FORTRAN, but this thread about "Private module procedures" seems to be topical, at least I hope so. Seems to contain answers, at least.


    jaredor summary:

    The public/private attribute exists within modules in Fortran 90 and later. Fortran 77 and earlier--you're out of luck.

    0 讨论(0)
  • 2021-02-07 12:35

    If you use modules, here is the syntax:

    PUBLIC  :: subname-1, funname-2, ...
    
    PRIVATE :: subname-1, funname-2, ...
    

    All entities listed in PRIVATE will not be accessible from outside of the module and all entities listed in PUBLIC can be accessed from outside of the module. All the others entities, by default, can be accessed from outside of the module.

    MODULE  Field
      IMPLICIT   NONE
    
      Integer :: Dimen
    
      PUBLIC  :: Gravity
      PRIVATE :: Electric, Magnetic
    
    CONTAINS
    
      INTEGER FUNCTION  Gravity()
        ..........
      END FUNCTION Gravity
    
    
      REAL FUNCTION  Electric()
        ..........
      END FUNCTION
    
    
      REAL FUNCTION  Magnetic()
        ..........
      END FUNCTION
    
      ..........
    
    END MODULE  Field
    
    0 讨论(0)
  • 2021-02-07 12:47
    Private xxx, yyy, zzz
    
    real function xxx (v)
      ...
    end function xxx
    
    integer function yyy()
      ...
    end function yyy
    
    subroutine zzz ( a,b,c )
      ...
    end subroutine zzz
    
    ... 
    other stuff that calls them
    ...
    
    0 讨论(0)
  • 2021-02-07 12:51

    This will only work with a Fortran 90 module. In your module declaration, you can specify the access limits for a list of variables and routines using the "public" and "private" keywords. I usually find it helpful to use the private keyword by itself initially, which specifies that everything within the module is private unless explicitly marked public.

    In the code sample below, subroutine_1() and function_1() are accessible from outside the module via the requisite "use" statement, but any other variable/subroutine/function will be private.

    module so_example
      implicit none
    
      private
    
      public :: subroutine_1
      public :: function_1
    
    contains
    
      ! Implementation of subroutines and functions goes here  
    
    end module so_example
    
    0 讨论(0)
提交回复
热议问题