- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
ENDLink Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page