How do I declare a private function in Fortran?
Statement and Attribute: Specifies that entities in a module can be accessed only within the module itself. The PRIVATE attribute can be specified in a type declaration statement or a PRIVATE statement, and takes one of the following forms: Syntax.
Public and private are public - any program that includes the module can see them. The keyword private means that only variables and subprograms with the public attribute are available to the including program. Return to Fortran 90 index.
A function subprogram is a complete, separate program from the main program that computes a single value that is returned to the main program in the function name. A function subprogram may contain any FORTRAN statement. It is composed of the function name, its argument list, a RETURN statement and an END statement.
A procedure is a group of statements that perform a well-defined task and can be invoked from your program. Information (or data) is passed to the calling program, to the procedure as arguments.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With