2014-10-02 77 views
0

任何人都可以告诉我为什么这个简单的程序给我一个分段错误?如何在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, y 
real z 
real c 

z = c(x,y) 
end 
+1

我试过了,在移动固定格式代码'$ gcc tst.for -o tst -lgfortran &&。/ tst'后回来了'100.000000' Fedora 19,AMD – 2014-10-02 21:27:39

+0

似乎对我很好,但请注意你的子程序在你的程序中是* not *,它们是* external *,这是是相反的。 – 2014-10-03 04:34:07

回答

0

不如把你的程序到一个模块中,使它们的接口都互相认识和任何程序或程序use荷兰国际集团的模块:

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

相关问题