Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran Compiler Terminology: Dummy Variables and Attributes

Can someone explain to me what sort of an abstraction in the parser / compiler a dummy variable or attribute corresponds to?

      PURE SUBROUTINE F(X, Y)
        IMPLICIT NONE
        REAL, INTENT(INOUT) :: X, Y, C
C        REAL :: A, B
C        REAL, SAVE :: C = 3.14E0
        PARAMETER (C = 3.14E0, X = 32, Y = X)
        X = Y + 2 * SIN(Y)
      END

cetin@unique:~/lab/secret/tapenade$ gfortran -x f77 -c 1.f 
1.f:6.37:

        PARAMETER (C = 3.14E0, X = 32, Y = X)                           
                                    1
Error: PARAMETER attribute conflicts with DUMMY attribute in 'x' at (1)
1.f:3.38:

        REAL, INTENT(INOUT) :: X, Y, C                                  
                                     1
Error: Symbol at (1) is not a DUMMY variable

cetin@unique:~/lab/secret/tapenade$ ifort -c 1.f
1.f(3): error #6451: A dummy argument name is required in this context.   [C]
        REAL, INTENT(INOUT) :: X, Y, C
-------------------------------------^
1.f(6): error #6406: Conflicting attributes or multiple declaration of name.   [X]
        PARAMETER (C = 3.14E0, X = 32, Y = X)
-------------------------------^
1.f(6): error #6406: Conflicting attributes or multiple declaration of name.   [Y]
        PARAMETER (C = 3.14E0, X = 32, Y = X)
---------------------------------------^
1.f(6): error #6592: This symbol must be a defined parameter, an enumerator, or an argument of an inquiry function that evaluates to a compile-time constant.   [X]
        PARAMETER (C = 3.14E0, X = 32, Y = X)
-------------------------------------------^
compilation aborted for 1.f (code 1)
like image 760
Cetin Sert Avatar asked Jun 28 '09 05:06

Cetin Sert


2 Answers

Fortran passes by reference. The dummy attribute corresponds to those variables that are passed in to the function (X and Y in your case). The parameter statement is expecting something static, but since X is whatever is passed into the function, it really doesn't make any sense. The parameter statement is a way to set up constants - it doesn't have anything to do with the parameters to a subroutine.

When you get the error saying that C is not a DUMMY variable, then, it means that it's not finding C in the list of variables that will be passed into/out of the function - your declaration is only F(X, Y): no C in sight. Though that you are not using the DUMMY attribute explicitly, you have the INTENT(INOUT) attribute, which means that these variables correspond to subroutine input/output.

To get what you want, you would have a subroutine that looks something like:

subroutine F(X, Y)
    implicit none

    ! These are the dummy variables
    real, intent(inout) :: X, Y

    ! These are the variables in the scope of this subroutine
    real                  :: a, b
    real, parameter, save :: c = 3.14E0

    X = Y + 2*sin(Y)
end subroutine F

I'm not entirely sure what you are trying to do - you're declaring a pure subroutine, which means a subroutine without side effects, but you are using intent(inout) for your variables, which means that X and Y can be altered in the course of execution.

I'd add as well that inside a subroutine, initializing a variable in its declaration statement like REAL :: C = 3.14E0 yields a variable with an implicit save attribute. If you want it to be saved from call to call, though, you've done the right thing by explicitly adding the save attribute to make it clear that that's what you're doing.

I'm not a parser/compiler guy, but I think that to answer your question, the dummy attribute means that you're just getting a pointer - you don't have to allocate any space, since the variable used in the function call already has space allocated.

like image 170
Tim Whitcomb Avatar answered Nov 20 '22 15:11

Tim Whitcomb


The actual problem with the call is well explained by Tim Whitcomb. I will try to explain more explicitly the terms.

Dummy argument is a Fortran specific term. It is what other languages call formal parameters or similar, i.e. it is the object that is called X and Y (in your case) and witch gets associated to an actual argument when the procedure is called.

Therefore in:

subroutine s(i)
  integer :: i
end

call s(1)

the i is a dummy argument of the subroutine s while the expression 1 is the actual argument which is passed to the subroutine to the dummy argument i.

Attributes are a form of specifying additional properties of data objects or procedures. The attributes can be specified using statements:

real c
intent(in) c
optional c

or they can be specified in a single declaration:

real, intent(in), optional :: c

This way the dummy argument c is a default real with attributes intent(in) and optional.

Conflicting attributes are attributes that can't be specified for one object at the same time. Your example with intent(...) and parameter serves well. These are incompatible as the first implies a dummy argument and the other specifies a named constant.

like image 44
Vladimir F Героям слава Avatar answered Nov 20 '22 15:11

Vladimir F Героям слава