如何在Fortran中使用子例程中的函数?

如何在Fortran中使用子例程中的函数?,fortran,Fortran,谁能告诉我为什么这个简单的程序会给我一个分段错误 我使用的是gfortran,我需要在子程序中使用一个函数 program tst implicit none real z,x,y x=10. y=2. call s(10.,10.,z) print*,z end program real function c(x,y) implicit none real x, y c = x*y return end subroutine s(x,y,z) implicit none real x,

谁能告诉我为什么这个简单的程序会给我一个分段错误

我使用的是gfortran,我需要在子程序中使用一个函数

program tst
implicit none

real z,x,y
x=10.
y=2.
call s(10.,10.,z)
print*,z

end program

real function c(x,y)
implicit none
real x, y
c = x*y
return
end

subroutine s(x,y,z)
implicit none
real x, y
real z
real c

z = c(x,y)
end

最好将您的过程放入一个模块中,以便彼此以及使用该模块的任何过程或程序都知道它们的接口:

module MyStuff

contains

function c(x,y)
implicit none
real :: c
real :: x, y
c = x*y
return
end

subroutine s(x,y,z)
implicit none
real :: x, y, z

z = c(x,y)
end

end module MyStuff

program tst
use MyStuff
implicit none

real z,x,y
x=10.
y=2.
call s(10.,10.,z)
print*,z

end program

它可以工作,给出100的答案。

我尝试了它,在转换了固定格式$gcc tst.for-o tst-lgfortran的代码后&./tst返回100.000000 Fedora 19,AMDSeems对我来说没问题,但是请注意,您的子程序不在程序内部,它们是外部的,这是相反的。