PRB: Passing Constants to Subroutines in FORTRAN

Last reviewed: December 11, 1995
Article ID: Q35871
The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 4.0 and 4.1
  • Microsoft FORTRAN PowerStation, versions 1.0 and 1.0a
  • Microsoft FORTRAN PowerStation 32, version 1.0 and 4.0

SYMPTOMS

The code below will generate the following output:

        1.000000
        2.000000

or print 1.000000 and cause a general protection fault.

CAUSE

This behavior is not a problem with Microsoft FORTRAN. The FORTRAN 77 ANSI standard, section 15.9.2, specifies that when passing a constant as an actual argument to a subroutine, the associated dummy argument (in this case x) cannot be modified.

Microsoft FORTRAN does not generate error messages while compiling. The above code does not follow the standard; therefore, it will generate unpredictable results because FORTRAN passes by reference.

RESOLUTION

If the associated dummy argument is going to be modified, pass a variable. For example, use "call sub1(y)" with y=1.

MORE INFORMATION

The following code demonstrates this information:

        write (*,*) 1.0
        call sub1(1.0)
        write (*,*) 1.0
        end

        subroutine sub1(x)
        real x
        x = 2.0
        return
        end


Additional reference words: 1.00 4.00 4.10 5.00 5.10
KBCategory: kbprg kbprb
KBSubcategory: FORTLngIss


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 11, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.