Re: operation on module arguments
- From: Paul van Delst <Paul.vanDelst@xxxxxxxx>
- Date: Thu, 21 Sep 2006 18:56:47 -0400
gottoomanyaccounts@xxxxxxxxx wrote:
dpb wrote:gottoomanyaccounts@xxxxxxxxx wrote:Hi there,...
I have a question about how I can update the variables declared on a
module by simply having the USE statement only....
The point is, I want c always to be the sum of a and b. In the main
program, a and b may be read in from an external file, (in the example
above, they get explicitly set.) What I want is, when a and b get
changed, c is automatically updated, I don't have to call another
subgrogram to update c. All I would like to have is just the 'use foo'
statement in the main program.
As Richard points out, there's no functionality like that in Fortran so
you can't achieve the stated object w/o adding something else to the
code. But, you can certainly add the necessary code to be executed at
each point in the program where either a or b or both are modified. In
your example, if there is a READ statement that includes either, then
after that and before c is referenced elsewhere simply include the
statement
c = a + b and that will accomplish the task.
This is what I originally thought and indeed the motivation I posted
this question. Since a , b , and c are defined together in one module,
when any of them gets modified (by whatever method) in the main
program, others should be able to updated automatically, by this way, I
kind of think the module is self-defined. If I have to impose the
update manually, the concept of module gets a little bit less elegant
(this is only my own opinion, may not make sense to any one here :)).
It's late and I may be a bit thick, but how are you supposed to specify what update expression to use?
What if you have:
module foo
implicit none
real :: a, b, c
contains
function add(a,b) result(c)
real, intent(in) :: a, b
real :: c
c = a+b
end
function sub(a,b) result(c)
real, intent(in) :: a, b
real :: c
c = a-b
end
function mult(a,b) result(c)
real, intent(in) :: a, b
real :: c
c = a*b
end
....etc...
end module foo
if you then do:
program main
use foo
implicit none
a= 1.0
b= 9.0
write(*,*) c
end program
Which expression do you want to use to update c? Add, subtract or multiplication? You say you want to add... how is the guy in the next cubicle, who want to do the same thing except subtract a and b, to specify his requirement? Computers and the programs that run on them are imbued with only as much intelligence as we the programmers supply. If you don't tell it to c=a+b it's not gonna know.
This reminds me of that scene in one of the Star Trek movies (the whale rescue one) where Scottie sits down in front of an old Mac and says <brogue>"Computer"</brogue>, expecting a response. :o)
cheers,
paulv
--
Paul van Delst Ride lots.
CIMSS @ NOAA/NCEP/EMC Eddy Merckx
Ph: (301)763-8000 x7748
Fax:(301)763-8545
.
- Follow-Ups:
- Re: operation on module arguments
- From: gottoomanyaccounts
- Re: operation on module arguments
- References:
- operation on module arguments
- From: gottoomanyaccounts
- Re: operation on module arguments
- From: dpb
- Re: operation on module arguments
- From: gottoomanyaccounts
- operation on module arguments
- Prev by Date: Re: operation on module arguments
- Next by Date: Re: operation on module arguments
- Previous by thread: Re: operation on module arguments
- Next by thread: Re: operation on module arguments
- Index(es):