Re: operation on module arguments




Paul van Delst wrote:
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.

Maybe I didn't describe very clearly. The relation of c to a and b is
of course supplied by me in the module which defines a, b, and c. If I
want c=a+b, then it is c=a+b, if I want c=a**b, then it should be
c=a**b. But the fact I understand is, even if I defined the relation in
the module, such as

module foo
real :: a, b, c

contains
subroutine getc
c = a + b
end subroutine getc
end module foo

Then in the main program, when a or b is modified, I still need to
explicitly
call getc
to update c. What I want is how I can write a module foo such that in
the main program, when a or b is modified, c is updated automatically,
so I can get rid of "call getc".
The original movitation is that, I have three variables a, b, and c.
The variables a and b are initially read from an external file, and get
changed later on in the main program, but the variable c is so closely
related to a and b, and it plays the same pole as a and b anywhere
else. Thus it is natural to declare a, b, and c in a module and their
relation in this module.
If the cocept of "module as a black box" really applies, then there
should be a mechanism to update c upon a and b just inside their host
module. If I have to manually update c=a+b outside the module, then I
won't bother to define c in module foo, instead I can declare it in the
main program, but this makes the main program less elegant because c is
so close to a and b and I really want put it the same place as a and
b............


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

.



Relevant Pages

  • Re: Design of modules
    ... If variable x is declared in module foo, ... >> declare the data that must be shared in the same module such that most ... >> of the subroutines will share most of the data in the module. ... >object that gets used throughout the program w/o explicit clutter of the ...
    (comp.lang.fortran)
  • Re: operation on module arguments
    ... end subroutine getc ... end module foo ... language is pretty reasonable, so I was not too greedy, was I? ... compilation time. ...
    (comp.lang.fortran)