Re: operation on module arguments



gottoomanyaccounts@xxxxxxxxx wrote:
I have a question about how I can update the variables declared on a
module by simply having the USE statement only. To illustrate, for
example

file foo.f90
--------------
module foo
implicite none
real :: a, b, c

**** (anything I need here to make c=a+b)

end module foo
=========================
file main.f90
----------------
program main
use foo
implicit none
a= 1.0
b= 9.0
write(*,*) c
end program
=========================

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.

module foo
implicit none
real :: a, b
contains
real function c()
c = a + b
end function
end module

program main
use foo
implicit none
a = 1.0
b = 9.0
write(*,*) c()
end program

Alternately, you could make a and b some sort of special user-defined type, and define an assignment operator for that type that also updates c when a value is assigned to a or b. However, this doesn't work if a and b are directly read in via a read statement.

- Brooks


--
The "bmoses-nospam" address is valid; no unmunging needed.
.



Relevant Pages

  • Re: operation on module arguments
    ... It's late and I may be a bit thick, but how are you supposed to specify what update expression to use? ... implicit none ... end module foo ... Add, subtract or multiplication? ...
    (comp.lang.fortran)
  • Re: map/filter/reduce/lambda opinions and background unscientific mini-survey
    ... b = foo(a) # error because a dissapears before foo gets it. ... to pass a neutral value of some sort, then you'd just have to pick something else and test for it. ... The technic for disabling this was baroque ("IMPLICIT BOOLEAN*1 A-Z"), but so common they invented a syntax for it in later versions of FORTRAN ("IMPLICIT ...
    (comp.lang.python)
  • Re: USE inside MODULE
    ... conflicts. ... module foo ... end module foo ... Has "main" the access to both foo and bar or only to foo? ...
    (comp.lang.fortran)
  • Re: IMPLICIT NONE (F2k8+/-)
    ... >>> IMPLICIT NONE should finally be made default beginning at F2k8. ... >> imported from modules without explicit declaration... ... > use foo, only: boo,goo ... David Jones ...
    (comp.lang.fortran)
  • Re: IMPLICIT NONE (F2k8+/-)
    ... > I have top admit to being bored by this discussion of IMPLICIT NONE. ... > from modules without explicit declaration... ... found that using the ONLY clause when importing from modules helps me ... use foo, only: boo,goo ...
    (comp.lang.fortran)