home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!uwm.edu!linac!unixhub!slacvx.slac.stanford.edu!fairfield
- From: fairfield@slacvx.slac.stanford.edu
- Newsgroups: comp.lang.fortran
- Subject: Re: Fortran 90: Pointers to Functions?
- Message-ID: <1992Aug14.103217.1@slacvx.slac.stanford.edu>
- Date: 14 Aug 92 18:32:17 GMT
- Article-I.D.: slacvx.1992Aug14.103217.1
- References: <166htnINNhvn@darkstar.UCSC.EDU> <1693l7INNn4o@darkstar.UCSC.EDU> <16gp3nINNbm8@darkstar.UCSC.EDU>
- Sender: news@unixhub.SLAC.Stanford.EDU
- Organization: Stanford Linear Accelerator Center
- Lines: 80
- Nntp-Posting-Host: slacvx.slac.stanford.edu
-
- In article <16gp3nINNbm8@darkstar.UCSC.EDU>, sla@fast.ucsc.edu (Steve Allen) writes:
- > In article <1693l7INNn4o@darkstar.UCSC.EDU> sla@fast.ucsc.edu I wrote:
- >>[Fortran 77 & 90 do not have anything like C's "pointer to a function"]
- >>Is there some other (reasonably elegant) method for getting this kind of
- >>functionality in Fortran 90?
- >
- > This is an example of what I would like to be doing with pointers to functions
-
- [example deleted]
-
- > Then, the thing that I would like to be able to do, which can be expressed
- > in many fewer lines of code.
- > SUCCESS = .FALSE.
- > I = 0
- > 100 CONTINUE
- > IF (.NOT. SUCCESS) THEN
- > IF (I .LT. 459) THEN
- > I = I + 1
- > IF (STATE .EQ. KNOWNSTATE(I)) THEN
- > CALL {the Ith function, we have no syntax for this}
- > SUCCESS = .TRUE.
- > ENDIF
- > GOTO 100
- > ELSE
- > CALL REPORT_UNKNOWN_STATE(STATE)
- > ENDIF
- > ENDIF
- >
- > Furthermore, with some kind of array of pointers to functions, I could
- > exchange one function in the array for another during the execution of
- > the program. Basically, the user pushes some button, and the program
- > starts behaving differently because it is calling different functions
- > in response to the user's input.
-
- Well, you _can_ do this with VAX Fortran ("Gurgle!"...to quote an earlier,
- unrelated posting... ;-).
-
- Turns out that in about 1981 when the LBL side of our collaboration
- starting writing the analysis code for my thesis experiment, they said,
- "Hey! We don't have to use those _ugly_ DUMMY subroutines, we can just
- store that addresses of the subroutines the user actually wants called in
- an array, and pass the array value to a subroutine which calls the subroutine!
- So Eleagant!" (So obscure to the uninitiated...but flexible...)
-
- It works in two steps: the user writes an INIT subroutine which has
- a bunch of statements like (structure is simplified here):
-
- SUB_ARRAY(NEXT) = %LOC(THE_NEXT_SUBROUTINE_I_WANT_CALLED)
- NEXT = NEXT+1
-
- The %LOC built-in stored the address of THE_NEXT_SUBROUTINE_I_WANT_CALLED
- in array SUB_ARRAY. Then from the main program there was a loop something
- like:
-
- DO I=1,LAST_SUB
- CALL DO_SUB(%VAL(SUB_ARRAY(I))
- ENDDO
-
- in which the %VAL built-in passed the _value_ stored in SUB_ARRAY(I) to
- DO_SUB (rather than the address of SUB_ARRAY(I) ), and DO_SUB was something
- like:
- SUBROUTINE DO_SUB(SUBR)
- CALL SUBR()
- RETURN
- END
-
- Sorry to be so long-winded here, but I just couldn't resist giving an
- example that appears to match exactly what Steve is trying to do ;-) In
- fact, I'd like to here from some of the compiler experts out there why
- something like this couldn't easily (relatively speaking) be made standard
- given that it has been possible to pass functions to subprograms for a long
- time? Is it just committee politics?
-
- Cheers, Ken
- --
- Dr. Kenneth H. Fairfield | Internet: Fairfield@Slacvx.Slac.Stanford.Edu
- SLAC, P.O.Box 4349, MS 98 | DECnet: 45537::FAIRFIELD (45537=SLACVX)
- Stanford, CA 94309 | BITNET Fairfield@Slacvx
- ----------------------------------------------------------------------------
- These opinions are mine, not SLAC's, Stanford's, nor the DOE's...
-