Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private function in Fortran

Tags:

fortran

How do I declare a private function in Fortran?

like image 804
Graviton Avatar asked Oct 21 '08 08:10

Graviton


People also ask

What is a private statement?

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.

What does public mean in Fortran?

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.

What is function subprogram in Fortran?

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.

What is procedure in Fortran?

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.


2 Answers

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
like image 163
Tim Whitcomb Avatar answered Sep 24 '22 00:09

Tim Whitcomb


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
like image 30
Zeus Avatar answered Sep 21 '22 00:09

Zeus