Fortran子例程/函数参数名称和声明 [英] Fortran subroutine/function arguments names and declaration

查看:495
本文介绍了Fortran子例程/函数参数名称和声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于传递给Fortran子例程/函数的参数有一些一般性问题,尤其是在涉及非局部变量的命名时.

I have some general questions regarding the arguments passed to a subroutine/function in Fortran, in particolar when it comes to naming of non-local variables.

给出这个主程序

program xfunc
   implicit none
   real, dimension(5) :: var1, var2
   integer, var3
  ...
  call my_subroutine(var1(i),var2,var3)

SUBROUTINE my_subroutine(arg1,var2,arg3)
    !inout variable not matching the var1 declared in main
    real, intent(inout) :: arg1
    !matches the name and dimension of variable in main, is this needed?
    real, intent(inout), dimension(5) :: var2
    !should arg3 be named var3 since it overwrites the values in var3? And should arg3 have a corresponding variable in the main program
    integer, intent(out) :: arg3
    
end my_subroutine

  1. 在声明中,名称只是标签",对吗?它们不需要匹配主程序中变量的名称.
  2. 参数的类型维度也不需要与主程序中的参数匹配,对吗?因此,在inout变量的情况下,子例程中的arg1(数组)也可以是实数吗?他们只需要匹配子例程中的声明?这仅适用于intent(in)参数吗?
  3. 即使变量是"inout",也需要在子例程中声明变量吗?并准确匹配主程序中的内容?
  4. 命名子例程或函数的参数和变量时的优良作法是什么?是否应使用不同的名称将它们与主程序区分开?我对这个问题感到好奇,尤其是对于所有变量(从内到外).

推荐答案

这是关于参数关联的问题.争论的意图在很大程度上与这次讨论无关.

This is a question about argument association. Intent of arguments is largely irrelevant to this discussion.

在过程(子例程或函数)声明中,标签"是将过程作为其范围的实体的名称.在问题的my_subroutine声明中,这些实体的名称为arg1var2arg3.这些是该过程的伪参数.这些伪参数的名称与其他作用域中的实体名称完全无关(或多或少,不是该过程的任何东西).参数意图不会影响命名或关联.

In the procedure (subroutine or function) declaration, the "labels" are the names of entities which have the procedure as their scope. In the declaration of my_subroutine of the question these entities have names arg1, var2 and arg3. These are the dummy arguments of the procedure. The names of these dummy arguments are entirely unrelated to the names of entities in other scopes (more or less, anything that isn't that procedure). Argument intent does not affect the naming or association.

其他范围内的实体(例如问题的主程序)是不同的事物.但是,当存在过程引用(例如子例程调用)时,在引用位置的参数(实际参数)与该过程本地的实体(再次是伪参数)之间建立了关联.名称不是这种关联的一个因素:实际参数和伪参数不需要具有相同的名称.确实(无论意图如何),实际参数甚至都不必是名称.

Entities in other scopes, such as the main program of the question, are different things. However, when there is a procedure reference, such as the subroutine call, there is established an association between the arguments in the referencing place (the actual arguments) and the entities local to the procedure (again, the dummy arguments). Name is not a factor in this association: the actual arguments and the dummy arguments do not need to have the same name. Indeed (and regardless of intent), the actual arguments don't even need to be names.

在每个范围(主程序,过程)中,都使用了相应的名称.

Within each scope (main program, procedure) the corresponding names are used.

作为参数关联的一部分,我们不能简单地说主程序中的参数1与过程中的参数1相同".这样做有很多方面,但是我们可以在这里说这(响应子问题2):虚拟和实际参数通常不需要具有相同的形状(元素数量或等级).在许多情况下,形状都有限制.意图不是这里的因素.在过程内部,伪参数具有其声明的特征,独立于(除了可能存在的任何匹配限制之外)实际参数的特征.

As part of the argument association, we don't simply say "argument 1 in the main program is the same thing as argument 1 in the procedure". There are many aspects to this, but we can say here that this means (in response to sub-question 2): dummy and actual arguments in general don't need to have the same shape (either number of elements, or rank). In many cases there are restrictions on shape. Intent is not a factor here. Inside the procedure the dummy argument has the characteristics of its declaration, independent (except for any matching restrictions which may be in place) of the characteristics of the actual argument.

此外,可能有一个数组元素实际参数对应于一个数组虚拟参数(但不是标量实际参数).这使用了存储关联,您可以在此处找到许多其他问题.

Further, it's possible to have an array element actual argument correspond to an array dummy argument (but not a scalar actual argument). This uses storage association about which you can see many other questions here.

相反,对于非基本过程,不可能有一个数组实际参数与标量伪参数相对应.

On the contrary, it isn't possible to have an array actual argument correspond to a scalar dummy argument for non-elemental procedures.

如果实体不是过程的伪参数,则必须存在某种其他形式的关联才能使主程序的实体在过程范围内可访问.这可以是 host 关联, use 关联,存储关联(或 linkage 关联).无论如何,如果没有声明为伪参数,则实体不是伪参数.同样,意图在这里不起作用.

If an entity is not a dummy argument of a procedure then there must be some other form of association to get the entity of the main program accessible inside the scope of the procedure. This could be host association, use association, storage association (or linkage association). In any event, without declaration as a dummy argument the entity is not a dummy argument. Again, intent plays no part here.

我不会谈第4个子问题(相当主观),只是说当不同作用域中的实体使用不同的名称时,帮助用户调试有问题的代码要容易得多:函数x >,它与主程序中的实际参数x关联,并且在使用中与bar的模块变量x关联."保持直调有点繁琐.

I won't touch on sub-question 4 (being rather subjective) except to say that helping users debug problematic code is much easier when entities in different scopes have different names: "the dummy variable x of function foo, which is argument associated with actual argument x in the main program, which is in term use associated with module variable x of bar" gets a bit tedious to keep straight.

这篇关于Fortran子例程/函数参数名称和声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆