Home > Archive > Fortran > March 2007 > Function kind attribute: valid fortran?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Function kind attribute: valid fortran?
|
|
| Bil Kleb 2007-03-16, 7:07 pm |
| I have discovered that some compilers think
the following code is ok while others do not.
module kinds
integer, parameter :: dp = selected_real_kind(15)
end module kinds
module test_undeclared_kind; contains
REAL(DP) function declared_dp_before_defined()
use kinds, only: dp
declared_dp_before_defined = 1.0_dp
end function
end module
The issue is that "DP" is not specified before it
is used. To satisfy all compilers, I need something like,
function declared_after_dp_defined()
use kinds, only: dp
real(dp) :: declared_after_dp_defined
or put the 'use kinds, only: dp' in the module header.
Verdict?
Regards,
--
Bil Kleb
http://funit.rubyforge.org
| |
| Michael Metcalf 2007-03-16, 7:07 pm |
|
"Bil Kleb" <Bil.Kleb@NASA.gov> wrote in message
news:etej5o$i34$1@aioe.org...
>I have discovered that some compilers think
> the following code is ok while others do not.
>
The code is legal but this has been a consistent area of weakness. Moral:
don't do it.
Regards,
Mike Metcalf
| |
| Richard Maine 2007-03-16, 7:07 pm |
| Bil Kleb <Bil.Kleb@NASA.gov> wrote:
> I have discovered that some compilers think
> the following code is ok while others do not.
....
> REAL(DP) function declared_dp_before_defined()
> use kinds, only: dp
> The issue is that "DP" is not specified before it
> is used. To satisfy all compilers, I need something like,
....
> function declared_after_dp_defined()
> use kinds, only: dp
> real(dp) :: declared_after_dp_defined
Yeah. Frankly, I forget the exact ruling on that. I think there is a
special-case exception to allow your original case, but if so, it is a
special case... and I could believe that some compilers might miss it.
I personally prefer the style of the second version above. It seems more
consistent and robust. I basically never declare the type directly in
the function statement. I don't like the need for special cases... and
the special cases for sure don't cover every possibility. I could have
the fine points of the special case wrong, but if I recall it correctly,
your case is ok, but there are other, more complicated ones that are not
ok. For example, I'm thinking about things like
real(some_kind) function foo(...)
use kinds, only: dp
integer, parameter :: some_kind = an_expression_referencing_dp
As I said, I could have this all . I personally use a style
where I don't have to memorize the exact rules fo rthe special-case
exceptions.
Note that there are some things that the syntax doesn't allow you to
declare in the function statement. Examples include array properties,
pointer, and allocatable. If you use a style of always declaring the
function type in a type declaration statement, then those can be done
consistently. If you put the type in the function statement, then you
have to declare those other properties separately, which I find
inconsistent.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| Paul van Delst 2007-03-16, 7:07 pm |
| Bil Kleb wrote:
> I have discovered that some compilers think
> the following code is ok while others do not.
>
> module kinds
> integer, parameter :: dp = selected_real_kind(15)
> end module kinds
>
> module test_undeclared_kind; contains
>
> REAL(DP) function declared_dp_before_defined()
>
> use kinds, only: dp
>
> declared_dp_before_defined = 1.0_dp
>
> end function
>
> end module
>
> The issue is that "DP" is not specified before it
> is used. To satisfy all compilers, I need something like,
>
> function declared_after_dp_defined()
>
> use kinds, only: dp
>
> real(dp) :: declared_after_dp_defined
>
> or put the 'use kinds, only: dp' in the module header.
>
> Verdict?
My only objection (if you could call it that) is based on style/convention grounds. I
always prefer to:
a) Put USE statements in module headers, and
b) Declare the function in the function body
anyway.
I do (b) so my parsers for documention can be dumber and don't have to figure out that
there is no difference between,
real(dp) function f(w,x,y)
real(dp), intent(in) :: w,x,y
and
function f(w,x,y)
real(dp), intent(in) :: w,x,y
real(dp) :: f
or even
function f(w,x,y) result(z)
real(dp), intent(in) :: w,x,y
real(dp) :: z
My function declaration statements remain forever unadulterated (well, un-prefixed with
their type at least :o)
cheers,
paulv
--
Paul van Delst Ride lots.
CIMSS @ NOAA/NCEP/EMC Eddy Merckx
| |
| James Giles 2007-03-16, 7:07 pm |
| Bil Kleb wrote:
> I have discovered that some compilers think
> the following code is ok while others do not.
>
> module kinds
> integer, parameter :: dp = selected_real_kind(15)
> end module kinds
>
> module test_undeclared_kind; contains
>
> REAL(DP) function declared_dp_before_defined()
>
> use kinds, only: dp
I don't have time to look it up now, but I'm pretty
sure there is a specific rule allowing the above.
It's one of the arguments in favor of making the
type declaration in the FUNCTION statement
obsolescent that such an anomalous rule is required.
Personally I prefer to declare the type of a function
within the FUNCTION statement. But I recognize
that it's difficult and will get more so due to the fact
that there's more to a declaration than just type: attributes,
shape, etc.
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
|
|
|
|
|