Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29392 Discussions

Can't Define Statement Function?

Zhanghong_T_
Novice
1,127 Views
Hi all,
I found if I add 'implicit none' in code, my 'statement functioin' can't be compiled.
Can anyone tell me how to solve the problem?
Thanks,
Zhanghong Tang
Code:
PROGRAM SINES
implicit none
           DOUBLE PRECISION X, PI
           PARAMETER (PI=3.141592653589793238D0)
           COMMON V(3)

    !    Define SIN as a statement function 

           SIN(X) = COS(PI/2-X)
           DO X = -PI, PI, 2*PI/100

    !    Reference the statement function SIN 

             WRITE (6,100) X, V, SIN(X)
           END DO
   100     FORMAT (5F10.7)
         END
0 Kudos
6 Replies
Jugoslav_Dujic
Valued Contributor II
1,127 Views
Well, add
REAL Sin
to your declarations.
Statement functions are on obsolescent list of F90+ standards. Although useful at times, (acting much like C macros), they have quite quirky typing rules -- unlike C macros, they must be typed, and you have to declare their type separately from their body, as above. If I recall correctly, you can't have them in a MODULE for some of typing/scoping reasons. Internal functions (behind CONTAINS statement) are the preferred "F90-style".
Jugoslav
0 Kudos
Steven_L_Intel1
Employee
1,127 Views
Actually, they follow the same typing rules as anything else. If you have IMPLICIT NONE, you must explicitly declare the type - you don't get a free ride because an intrinsic exists of the same name. Because a statement function is just a single line, if you're going to give it a type it must be done separately.
I agree that statement functions should not be used in new code; contained procedures are much better, flexible and easier to understand.
0 Kudos
Zhanghong_T_
Novice
1,127 Views
Thank you very much, Jugoslav!
I think a statement function will not restric the data type. That is to say, It can return to any data type (integer, real, real*8) as user need (the input data X is similar), because it is not a real function, when compiling, compiler will simplily replace the statement function with its expression.
Zhanghong Tang
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,127 Views
I'm not sure we agree -- unlike C macros, statement functions do have a type, as well as their arguments. If you don't use IMPLICIT NONE, their return valuegets the implicit type by their name. Thus:
Code:
implicit none
integer iAdd, a, b, j

iAdd(a,b) = a + b

j = iAdd(7.9, 2.5)
write(*,*) j  !Returns 9
I find a bit odd that CVF does not complain about argument mismatch (integer vs. real) but I'm not sure what the Standard says about it either. Apparently, implicit conversion takes place before execution (i.e. it's INT(7.9) + INT(2.5)).
Still, it's much better to use internal routines instead.
Jugoslav
0 Kudos
Steven_L_Intel1
Employee
1,127 Views
You'll get an argument mismatch if you compile with standards warnings enabled. The compiler has an extension where it will convert values when it knows what to do. In some contexts, though, it's more forgiving than I like to see....
0 Kudos
Zhanghong_T_
Novice
1,127 Views
It seems I misunderstand the function of statement function. I just think it like C's macros. Compiler only simply replace the statement functionwith its expression. The input and output data is decided by how it is called.
Thank you again, Jugoslav and Steve!
Zhanghong Tang
0 Kudos
Reply