本文介绍了是否有必要在任何调用例程中声明用户定义函数的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Intel Visual Fortran.根据Chapmann的书,在调用它的例程中声明函数类型是NECESSARY.但是看看这段代码,

I use the Intel Visual Fortran. According to Chapmann's book, declaration of function type in the routine that calls it, is NECESSARY. But look at this piece of code,

module mod
implicit none
contains
function fcn ( i )
    implicit none
    integer :: fcn
    integer, intent (in) :: i
    fcn = i + 1
end function
end module

program prog
    use mod
    implicit none
    print *, fcn ( 3 )
end program

它在调用例程(此处为prog)中没有该声明的情况下运行,实际上,当我在程序prog或任何其他单元中定义其类型(我的意思是函数类型)时,它会出现此错误,

It runs without that declaration in the calling routine (here prog) and actually when I define its type (I mean function type) in the program prog or any other unit, it bears this error,

错误#6401:此名称的属性与USE语句可访问的属性冲突. [FCN] Source1.f90 15

error #6401: The attributes of this name conflict with those made accessible by a USE statement. [FCN] Source1.f90 15

我怎么了?还是如果我是对的,如何证明其合理性?

What is my fault? or if I am right, How can it be justified?

推荐答案

您必须使用查普曼著作的旧版本,否则可能会误解其内容.当然,调用例程必须知道被调用函数的类型,并且在Fortran-before-90中,程序员有责任确保调用函数具有该信息.

You must be working with a very old copy of Chapman's book, or possibly misinterpreting what it says. Certainly a calling routine must know the type of a called function, and in Fortran-before-90 it was the programmer's responsibility to ensure that the calling function had that information.

但是,由于90标准和module的引入,还有其他更好的方法可以将有关被调用函数的信息提供给调用例程.这些方法之一是将被调用的函数放入模块中并use关联该模块.当您的程序采用这种方法时,编译器会处理问题.这正是您的代码所做的,并且不仅正确,而且还是一种很好的方法,符合现代Fortran惯例.

However, since the 90 standard and the introduction of modules there are other, and better, ways to provide information about the called function to the calling routine. One of those ways is to put the called functions into a module and to use-associate the module. When your program follows this approach the compiler takes care of matters. This is precisely what your code has done and it is not only correct, it is a good approach, in line with modern Fortran practice.

关联是Fortran的标准说法,用于将名称(例如fcn)与实体(例如称为fcn的功能)关联的方式. use-association 是通过在程序单元中写入use module来实现的,从而使module中的所有名称可用于使用module的单元.一个简单的use语句使模块中的所有实体都以其模块定义的名称为人所知.可以通过only子句修改use语句,这意味着仅某些模块实体可用.可以在use语句中重命名各个模块实体,从而将不同的名称与模块实体相关联.

association is Fortran-standard-speak for the way(s) in which names (such as fcn) become associated with entities, such as the function called fcn. use-association is the way implemented by writing use module in a program unit, thereby making all the names in module available to the unit which uses module. A simple use statement makes all the entities in the module known under their module-defined names. The use statement can be modified by an only clause, which means that only some module entities are made available. Individual module entities can be renamed in a use statement, thereby associating a different name with the module entity.

如果在调用例程中包含(重新)声明被调用函数的类型,则会出现错误消息,因为编译器将只允许一个被调用函数类型的声明.

The error message you get if you include a (re-)declaration of the called function's type in the calling routine arises because the compiler will only permit one declaration of the called function's type.

这篇关于是否有必要在任何调用例程中声明用户定义函数的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 14:19