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

Behavior of statement functions

carlls
Beginner
647 Views
I am converting legacy code to f90+ and have a an issue....

I have a statement function in the form of

DS2(XT,YT,XM,YM) = ((XT-XM)/XOM)**2+((YT-YM)/YOM)**2

So I know I can write:

REAL(KIND=4) FUNCTION DS2(XT,YT,XM,YM)
REAL(KIND=4) XT,YT,XM,YM
DS2= ((XT-XM)/XOM)**2+((YT-YM)/YOM)**2
END FUNCTION DS2

But where does XOM,YOM get it's declaration/value in the statement function? Does it just assume values from surrounding code, ignoring the fact that "call parameters" don't include XOM,YOM?

Is this behavior defned by the standard at all?

Regards
Carl

0 Kudos
3 Replies
Steven_L_Intel1
Employee
647 Views
Yes, it gets the values of any variables visible at the point of the call. Yes, it is defined by the standard, though statement functions are a "deprecated" feature. You can make DS2 an internal function and it will "host associate" variables from the caller.

I don't understand your sample code, though. It seems you have both a statement function and a regular function named DS2.
0 Kudos
carlls
Beginner
647 Views
Yes, it gets the values of any variables visible at the point of the call. Yes, it is defined by the standard, though statement functions are a "deprecated" feature. You can make DS2 an internal function and it will "host associate" variables from the caller.

I don't understand your sample code, though. It seems you have both a statement function and a regular function named DS2.


Steve:

Thanks for the prompt reply...

My statement function was orginally: DS2(XT,YT,XM,YM) = ((XT-XM)/XOM)**2+((YT-YM)/YOM)**2

and the left hand side lacked the XOM, YOM values. So from what you have said replacing DS2(XT,YT,XM,YM) with DS2(XT,YT,XM,YM,XOM,YOM ) everywhere should be just fine, as XOM,YOM should be defined in that section of code.

As it was explained to me by local grey beards the statement function worked something along the lines of apreprocessormacro ....the classic example in Cwould be:

#define MIN(X,Y) ((X) < (Y) ? : (X) : (Y))

So what you have said makes sense.

Which means I could do thesame approach here using fpp
#define DS2(XT,YT,XM,YM) ( ((XT-XM)/XOM)**2+((YT-YM)/YOM)**2 )

The effect is basically "in-lining" the function call hopefully making for faster code, by eliminating a function call. Is thismacro definition a better approach ?


But now you mention ...`internal function and it will "host associate" variables from the caller`... this opens a few questions.

Internal functionhere I assume means thescope of the function name only be valid within a limited context, as in within a single F90+ module?

Thanks for you insight

Regards
Carl



0 Kudos
Steven_L_Intel1
Employee
647 Views
You can sort of think of a statement function like a macro, but it isn't, really. It is an internal function with the scope of the routine in which it is defined. You don't need to add variables to the argument list if you simply want the current values of XOM, YOM, etc. to be used.

I don't recommend using #define as that requires using the preprocessor and is not standard at all.

Internal procedures are not module procedures, though they look like that. You'd write something like this:

[plain]program main
real xom, yom
...
y = ds2(a,b,c,d)
...

contains
real function ds2 (xt,xm,yt,ym)
real xt,xm,yt,ym
ds2 =  (    ((XT-XM)/XOM)**2+((YT-YM)/YOM)**2 )
return
end function ds2
end program main[/plain]
When ds2 is called, it will "host associate" variables declared in the outer scope such as XOM and YOM and take whatever the current values are.
0 Kudos
Reply