Search code examples
segmentation-faultfortranfortran90double-precision

fortran 90 double precision argument of function


I tried to compile the following fortran90 code with gfortran, this program just output the value of a double precision function distance() which has 7 arguments. When I tried to run it I get the message "segmentation fault(core dumped)" but if I rewrite

distance(z1,z2,z3,z4,z5,13.0d0,z7) to distance(z1,z2,z3,z4,z5,z6,z7) where z6 has value 13.0d0 and recompile it I get the correct value 13.000...

Does anyone know why? They look identical to me. Thanks

program distancetest
implicit none
double precision::drand,distance
double precision::z1,z2,z3,z4,z5,z6,z7
!*********************************
z1=1.0d0
z2=1.0d0
z3=1.0d0
z4=4.0d0
z5=5.0d0
z6=13.0d0
z7=100.0d0
write(*,*) distance(z1,z2,z3,z4,z5,13.0d0,z7)
end program distancetest
!*********************************
double precision function distance(x1,y1,z1,x2,y2,z2,l)
implicit none
double precision::x1,x2,y1,y2,z1,z2,l,x,y,z
x1=x1-l*dble(floor(x1/l))
y1=y1-l*dble(floor(y1/l))
z1=z1-l*dble(floor(z1/l))
x2=x2-l*dble(floor(x2/l))
y2=y2-l*dble(floor(y2/l))
z2=z2-l*dble(floor(z2/l))
x=min(abs(x1-x2),l-abs(x1-x2))
y=min(abs(y1-y2),l-abs(y1-y2))
z=min(abs(z1-z2),l-abs(z1-z2))
distance=sqrt(x*x+y*y+z*z)
end function distance

Solution

  • The function modifies all of its dummy arguments, bar l. It is an error to associate anything other than a variable with dummy arguments that are modified. 13.0d0 is not a variable, z6 is.