Question

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
Was it helpful?

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.

OTHER TIPS

Extending the answer of IanH, if you put your procedures into modules and use those modules so that that the compiler can check consistency of arguments, and if you specify argument intents, then most Fortran compilers will find this problem for you.

module MyFunc
   implicit none
contains
   double precision function distance(x1,y1,z1,x2,y2,z2,l)
   implicit none
   double precision, intent (inout)::x1,x2,y1,y2,z1,z2
   double precision, intent (in) :: l
   double precision :: 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
end module MyFunc


program distancetest
use MyFunc
implicit none
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

For example, from gfortran:

write(*,*) distance(z1,z2,z3,z4,z5,13.0d0,z7)
                                   1
Error: Non-variable expression in variable definition context (actual argument to INTENT = OUT/INOUT) at (1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top