Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran: passing same array as two dummy arguments to subroutine

Suppose I have a subroutine which accepts two arrays as input. One is given intent(in) and the other is given intent(out). The latter is derived from the former in an arbitrary manner. However, what if I pass through the same actual argument for both dummy arguments? In general, the result will not be what was intended by the subroutine. See code snippet below.

The problem is, the compiler doesn't seem to care, even though I've given intent flags. I'm using Intel Visual Fortran Composer 12, with all diagnostics. Is there a better way of coding the subroutine, or some compiler option I'm missing, to make the code safer?

module foo

    contains

    subroutine sub_a()
        implicit none
        real::array(10,10)

        call sub_b(array,array)

    end subroutine

    subroutine sub_b(array1,array2)
        implicit none
        real,intent(in)::array1(10,10)
        real,intent(out)::array2(10,10)

        !array2 is derived from array1 somehow

    end subroutine

end module foo
like image 779
bdforbes Avatar asked Aug 22 '11 01:08

bdforbes


People also ask

How do you pass an array as a parameter in Fortran?

FORTRAN allows the use of adjustable dimensions in subprograms - the size of the array is passed as an argument in the list: DIMENSION SIG(15),ARR(10,10) N1=15 N2=10 N3=10 CALL SUBR3(NUM,SIG,N1,ARR,N2,N3,RESULT) .

Does Fortran pass by reference?

In general, Fortran routines pass arguments by reference. In a call, if you enclose an argument with the f77 and f90 nonstandard function %VAL() , the calling routine passes it by value.

What is a dummy argument in Fortran?

Dummy argument is a quantity in the specification of a procedure, function or subroutine. Actual argument is a quantity in the call of a procedure, function or subroutine. The call is made from the calling procedure or program unit, often from the main program.

What is an assumed shape array?

Assumed-shape arrays are dummy argument arrays where the extent of each dimension is taken from the associated actual arguments.


1 Answers

This is called aliasing -- referring to the same item by two different names. In most cases this is not allowed in Fortran. Your example is not legal Fortran -- see http://software.intel.com/en-us/blogs/2009/07/10/doctor-fortran-in-ive-come-here-for-an-argument-side-2/, which has this specific case, of aliasing via the same actual argument used for two dummy arguments. Fortran compilers are not required to diagnose all violations of the rules of the language and this appears to be an example that the compiler is not recognizing.

Edit: aliasing is permitted. What is forbidden is changing the value of the dummy argument through the alias, here the other dummy argument. The rules are less restrictive for pointer and target arguments. The specific rules are described in "The Fortran 2003 Handbook" by Adams et al.

like image 189
M. S. B. Avatar answered Nov 10 '22 07:11

M. S. B.