【发布时间】:2019-10-25 17:24:15
【问题描述】:
我一直在尝试为大学项目学习 Fortran 77,我在 Windows 10 中编写了以下代码:
program Q_Value
real BEs(4),Masses(4)
real Q,Tthr
integer i,Nuclei(4,2),A,Z
do 10 i=1,4,1
write(*,100)'Give the info for Nuclei:',i
100 format(A,I1)
write(*,*)'A='
read(*,*)Nuclei(i,1)
write(*,*)'Z='
read(*,*)Nuclei(i,2)
if((Nuclei(i,1).EQ.0).OR.(Nuclei(i,2).EQ.0))then
BEs(i)=0.
else
BEs(i)=BetheWeiss(Nuclei(i,1),Nuclei(i,2))
endif
Masses(i)=Mass(Nuclei(i,1),Nuclei(i,2),BEs(i))
10 continue
[...]
end
real function Mass(A,Z,BE)
integer A,Z
real BE,mass
c local var's
parameter(Mp=938.2720813,Mn=939.5654133)
c statements
Mass=((A-Z)*Mn)+(Z*Mp)-BE
return
end
在 GNU Fortran (gfortran 8.1.0) 中编译时出现以下错误:
Masses(i)=Mass(Nuclei(i,1),Nuclei(i,2),BEs(i))
1
Error: Return type mismatch of function 'mass' at (1) (INTEGER(4)/REAL(4))
谁能帮我解决这个问题,因为就我而言,我的函数返回一个实数,Masses(i) 是一个实变量。
【问题讨论】:
-
我想知道编译器是否正在处理函数内方程的整数元素的某种类型解析,例如'integer A,Z'以及它们在返回语句之前参与质量计算.也许 gnu fortran 有更严格的类型规则?
-
感谢您的回复@David W。我尝试在最后一个公式中用 dble 转换 A 和 Z(质量=....),但不幸的是没有任何改变。
-
好吧,我必须承认我的 fortran 已经过时了,但是如果你将 typedef 从函数中取出并确保返回类型的声明匹配 - 并且两者都指定了相同的大小写
-
你不想写
implicit integer (A-Z)而不是integer A,Z吗?但即使是,您真正想要的是所有程序单元(主程序、函数、子程序)中的implicit none。另外,强烈考虑本世纪的某些版本的 Fortran。请检查您是否准确复制了源代码。请将其设为minimal reproducible example,我们可以在没有任何[...]的情况下自行编译。 -
@Theodepastas 我终于找到了解决方案。发表了答案;在在线编译器上试了一下。
标签: function fortran gfortran type-mismatch