如何调用模块中子程序内部的函数?

发布于 2024-10-11 10:18:43 字数 466 浏览 0 评论 0原文

我有一个包含子例程的模块,该子例程又包含一个函数。我在主程序中说使用模块,并且可以调用子例程,但是如何访问子例程中包含的函数呢?

代码如下所示:

module useful
  integer, parameter :: N=2
  double precision, parameter :: xmin=1, xmax=10, pi=3.1415926535898
  double complex :: green(N,N), solution(N), k=(2.0,0.0)
contains
  subroutine y(n1)
  contains
    function x(n1)
      real :: n1, x
      x=n1*(xmax-xmin)/N
    end function x
  end subroutine y
end module useful

I have a module that contains a subroutine which in turn contains a function. I say use themodule in my main program and I can call thesubroutine, but how do I access the function that is contained in the subroutine?

The code looks like this:

module useful
  integer, parameter :: N=2
  double precision, parameter :: xmin=1, xmax=10, pi=3.1415926535898
  double complex :: green(N,N), solution(N), k=(2.0,0.0)
contains
  subroutine y(n1)
  contains
    function x(n1)
      real :: n1, x
      x=n1*(xmax-xmin)/N
    end function x
  end subroutine y
end module useful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉生梦死 2024-10-18 10:18:43

您不应在子例程中包含该函数。具有子程序后的功能。模块中的过程(子例程和函数)数量与您需要的数量一样多。每个都以子例程或函数语句开始,并以相应的结束语句结束。不要将它们嵌套在一起……而是一个接一个地嵌套。只有模块包含声明。然后从主程序或模块外部的过程“使用”该模块。

模块中的子程序和函数也可以相互访问。无需使用“包含”。

You should not contain the function inside the subroutine. Have the function after the subroutine. Just have as many procedures (subroutines & functions) in the module as you need. Start each with a subroutine or a function statement and end them with their corresponding end statement. Do not nest them inside each other ... instead, one after the other. Have only the module contains statement. Then "use" that module from your main program, or from a procedure outside of the module.

The subroutines and functions in the module are also accessible to each other. No need to use a "contains".

小红帽 2024-10-18 10:18:43

一些补充说明。
如果该函数仅由该子例程使用,则可以将该函数放在子例程中。在这种情况下,嵌套函数是一个有用的概念。

如果您想(永远)为外部程序隐藏模块中的某些函数,则可以在模块中将这些隐藏函数声明为私有。

module useful
public y,x ! shall be accessible by "use useful" statement in external program 
private ! anything else declared in the module is hidden for external program
integer, parameter :: N=2
!...

contains

subroutine y(n1)

end subroutine y

function x(n1)

end function x

end module useful

使用 public 和 private 可以帮助您避免使用语句污染名称空间的错误

use useful, only: y,x

use useful2, only: x,y,z

use useful3, only: x2,x3,x4

Some additional remarks.
You can put the function inside the subroutine, if this function is used by this subroutine only. In this case nesting the functions is a useful concept.

If you want to hide some functions in the module for the external program (forever), you declare those hidden functions as private in your module.

i.e

module useful
public y,x ! shall be accessible by "use useful" statement in external program 
private ! anything else declared in the module is hidden for external program
integer, parameter :: N=2
!...

contains

subroutine y(n1)

end subroutine y

function x(n1)

end function x

end module useful

using public and private helps you to avoid mistakes with contamination of your namespace by using the statements

use useful, only: y,x

use useful2, only: x,y,z

use useful3, only: x2,x3,x4
不知所踪 2024-10-18 10:18:43

为了澄清 MSB 给出的答案,请按如下方式拆分代码,注意如何从子例程 y() 中提取函数 x(),并且只有一个“包含”语句将模块级变量声明与函数/子例程声明分开:

module useful

  integer, parameter :: N=2
  double precision, parameter :: xmin=1, xmax=10, pi=3.1415926535898
  double complex :: green(N,N), solution(N), k=(2.0,0.0)

contains

  subroutine y(n1)
    real :: n1
    ! Here you can do something like:
    ! print 'F8.3', x(n1) 
  end subroutine y

  function x(n1)
    real :: n1, x
    x=n1*(xmax-xmin)/N
  end function x

end module useful

正如 MSB 指出的那样,x() 和 y() 位于同一范围内,因此从 y() 调用 x() 不需要执行任何特殊操作。

To clarify the answer given by M. S. B., split up your code as follows, noting how function x() has been extracted from subroutine y() and there's only a single 'contains' statement to separate module-level variable declarations from function/subroutine declarations:

module useful

  integer, parameter :: N=2
  double precision, parameter :: xmin=1, xmax=10, pi=3.1415926535898
  double complex :: green(N,N), solution(N), k=(2.0,0.0)

contains

  subroutine y(n1)
    real :: n1
    ! Here you can do something like:
    ! print 'F8.3', x(n1) 
  end subroutine y

  function x(n1)
    real :: n1, x
    x=n1*(xmax-xmin)/N
  end function x

end module useful

As M. S. B. points out, x() and y() are in the same scope so there's nothing special you need to do to call x() from y().

黯然#的苍凉 2024-10-18 10:18:43

要从子例程 y 调用函数 x,请

public :: x

contains 之前写入(右)。

To call function x from subroutine y, write

public :: x

(right) before contains.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文