Home > Archive > Fortran > February 2007 > external procedure
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 |
external procedure
|
|
|
| Hi,
In the following program, I need to
comment the line : " real, external :: func".
=============
module T
contains
real function ex(f,a)
real, external :: f
ex=f(a)
end function ex
real function func(num)
real :: num
func=num*2.
end function func
end module T
Program MAIN
use T
real, external :: func
print *,ex(func,3.)
END
===============
Why?
And if the functions ex and func are not in the module, then I need to
specify func in the main program as external.
Mike.
| |
| Richard Maine 2007-02-13, 4:10 am |
| Mike <acout@yam.com> wrote:
> In the following program, I need to
> comment the line : " real, external :: func".
....
> Why?
Because
1. It isn't external. Things don't generally work well when you tell the
compiler something that isn't true. Procedures come in 5 mutually
exclusive categories: external, internal, module, intrinsic, and
statement functions. If you try to label an internal procedure, module
procedure, intrinsic procedure, or statement function as external, then
it won't work.
(There are also dummy procedures. It is an unfortunate historical oddity
that you use the external attribute to declare a dummy procedure, even
though it isn't an external procedure).
Some people have gotten used to thinking of "external procedure" as a
synonym for user procedure. It isn't.
2. In any case, you can never redeclare things used from a module. The
USE statement brings in all the appropriate declarations. Even if the
redeclaration is accurate (which this one isn't - see 1 above), you
still can't do it.
> And if the functions ex and func are not in the module, then I need to
> specify func in the main program as external.
Yes. That's because
1. They are then external procedures.
2. You are using func in a context that requires declaration of its
property as being a procedure. That declaration comes either from a USE
statement or an external attribute.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| James Giles 2007-02-13, 4:10 am |
| Mike wrote:
> Hi,
>
> In the following program, I need to
> comment the line : " real, external :: func".
> =============
> module T
> contains
> real function ex(f,a)
> real, external :: f
> ex=f(a)
> end function ex
>
> real function func(num)
> real :: num
> func=num*2.
> end function func
> end module T
> Program MAIN
> use T
> real, external :: func
> print *,ex(func,3.)
> END
> ===============
> Why?
> And if the functions ex and func are not in the module, then I need to
> specify func in the main program as external.
Since FUNC is already specified in the module, an additional
declaration in the main program overrides the declaration in
the module. That's all part of the usual rules for USE association.
--
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
| |
| glen herrmannsfeldt 2007-02-13, 4:10 am |
| Richard Maine wrote:
(snip)
> (There are also dummy procedures. It is an unfortunate historical oddity
> that you use the external attribute to declare a dummy procedure, even
> though it isn't an external procedure).
As far as I know, you normally don't need to declare dummy procedures
as EXTERNAL (the real, external :: f in the OP's post). At least in
Fortran 66 you didn't, and I don't believe that you need to now.
The one case you do need to declare it EXTERNAL is if it is used
(or maybe only used) as an actual argument, in which case there is no
other way for the system to know that it is a dummy procedure.
It seems to me that a side effect of that requirement is that you are
allowed to declare a dummy procedure EXTERNAL, even if not used as
an actual argument.
-- glen
| |
| Richard Maine 2007-02-13, 4:10 am |
| glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote:
> Richard Maine wrote:
> (snip)
>
>
> As far as I know, you normally don't need to declare dummy procedures
> as EXTERNAL...
> The one case you do need to declare it EXTERNAL is...
Correct. At least that is one case. I don't recall whether there might
be others; wouldn't surprise me. I consider it recommended practice to
do so in all cases - not just those where you are required to. So the
list of places where you are required to is only of academic interest to
me.
> It seems to me that a side effect of that requirement is that you are
> allowed to declare a dummy procedure EXTERNAL, even if not used as
> an actual argument.
I don't understand that at all. Doesn't seem like a side effect, or even
related to me. As far as I can see...
1. If you want to or need to declare a dummy procedure, you use the
external attribute (or in f2003, a procedure statement).
2. There are cases where you need to declare a dummy procedure.
I see no relationship between these two things. The fact that sometimes
you need to declare a dummy procedure doesn't seem to me to have
anything to do with the fact that the external attribute is how you do
it, which was the point I was making.
Nor does it seem to me that the fact that you are required to declare it
in some cases has much to do with the fact that you are allowed to
declare it in others. I suppose one might argue for some connection
there, but that doesn't have anything to do with it being spelled
"external".
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| glen herrmannsfeldt 2007-02-13, 4:10 am |
| Richard Maine wrote:
(snip)
> Nor does it seem to me that the fact that you are required to declare it
> in some cases has much to do with the fact that you are allowed to
> declare it in others. I suppose one might argue for some connection
> there, but that doesn't have anything to do with it being spelled
> "external".
It isn't as bad as the uses for the keywords static and extern in C.
PL/I doesn't do too bad. EXTERNAL is the attribute for global
(more or less) variables, INTERNAL being the other choice.
The attribute corresponding to Fortran's EXTERNAL is ENTRY
(not to be with the ENTRY statement). The ENTRY
attribute works for both external and internal procedures,
and allows for ENTRY variables.
-- glen
| |
| Jan Vorbrüggen 2007-02-13, 4:10 am |
| >>It seems to me that a side effect of that requirement is that you are
> I don't understand that at all. Doesn't seem like a side effect, or even
> related to me. As far as I can see...
I read Glen's comment to mean: One needs the EXTERNAL to be able to properly
declare dummy procedures - a procedure used as an actual argument. This then
gives me the capability to gratuitously, so to speak, declare EXTERNAL any
other procedure, even if it is not used as an actual argument, that (loosely
speaking) will be brought in by the linker, at least in some cases, or maybe
nothing happens as a result of this gratuitous declaration.
Jan
|
|
|
|
|