Search code examples
functionfortransubroutinefortran95

I don't understand the output of a function in a Fortran95 code


I just got started with fotran95; I was given a code and I am studying it; I came across a subroutine that calls a function but I don't understand what the output is:

here is the subroutine:

SUBROUTINE collisione(vga, ga, vgb, gb)

IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(3), INTENT(INOUT) :: ga, gb    
DOUBLE PRECISION, DIMENSION(3), INTENT(INOUT) :: vga, vgb  

DOUBLE PRECISION, DIMENSION(3)   :: r, ra, rb, r_r

ra = pos_ini(ga)
rb = pos_ini(gb)


END SUBROUTINE

here is the function:

FUNCTION pos_ini(g)

IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(3) :: pos_ini
DOUBLE PRECISION, DIMENSION(3), INTENT(IN) :: g

DOUBLE PRECISION, DIMENSION(3) :: es, eg, es_p
DOUBLE PRECISION :: rnd, b, kos, ang 

CALL RANDOM_NUMBER(rnd)
b = 1.D0 - 2.D0*rnd 
kos = SQRT(1.D0 - b*b)
CALL RANDOM_NUMBER(rnd)
ang = pi2 * rnd

es(1) = kos * COS(ang)
es(2) = kos * SIN(ang)
es(3) = b

eg = g / SQRT(DOT_PRODUCT(g,g))

es_p = es - DOT_PRODUCT(es,eg) * eg

pos_ini = d * es_p/SQRT(DOT_PRODUCT(es_p,es_p)) ! IS THIS THE OUTPUT THAT GIVES THE VALUE OF ra and rb???????

END FUNCTION

(read the comment). So here comes my question: in the subroutine I see that the variables ra and rb are defined after using the function pos_ini where the input is the vector ga or gb. In the function pos_ini however I don't understand what the output is; is it pos_ini = d * es_p/SQRT(DOT_PRODUCT(es_p,es_p)) ?? if so why? and how come there is no intent(OUT) in function pos_ini?


Solution

  • In Fortran the returned function value is connected to the function name or a variable in the optional result clause. Therefore the function value will be whatever happened to be the value of pos_ini at the end of the function.

    In your case it is the espression d * es_p/SQRT(DOT_PRODUCT(es_p,es_p)).

    This is covered by any Fortran tutorial.