home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / fortran / 3041 < prev    next >
Encoding:
Internet Message Format  |  1992-08-14  |  3.7 KB

  1. Path: sparky!uunet!ogicse!uwm.edu!linac!unixhub!slacvx.slac.stanford.edu!fairfield
  2. From: fairfield@slacvx.slac.stanford.edu
  3. Newsgroups: comp.lang.fortran
  4. Subject: Re: Fortran 90:  Pointers to Functions?
  5. Message-ID: <1992Aug14.103217.1@slacvx.slac.stanford.edu>
  6. Date: 14 Aug 92 18:32:17 GMT
  7. Article-I.D.: slacvx.1992Aug14.103217.1
  8. References: <166htnINNhvn@darkstar.UCSC.EDU> <1693l7INNn4o@darkstar.UCSC.EDU> <16gp3nINNbm8@darkstar.UCSC.EDU>
  9. Sender: news@unixhub.SLAC.Stanford.EDU
  10. Organization: Stanford Linear Accelerator Center
  11. Lines: 80
  12. Nntp-Posting-Host: slacvx.slac.stanford.edu
  13.  
  14. In article <16gp3nINNbm8@darkstar.UCSC.EDU>, sla@fast.ucsc.edu (Steve Allen) writes:
  15. > In article <1693l7INNn4o@darkstar.UCSC.EDU> sla@fast.ucsc.edu I wrote:
  16. >>[Fortran 77 & 90 do not have anything like C's "pointer to a function"]
  17. >>Is there some other (reasonably elegant) method for getting this kind of
  18. >>functionality in Fortran 90?
  19. > This is an example of what I would like to be doing with pointers to functions
  20.  
  21.     [example deleted]
  22.  
  23. > Then, the thing that I would like to be able to do, which can be expressed
  24. > in many fewer lines of code.
  25. >     SUCCESS = .FALSE.
  26. >     I = 0
  27. > 100     CONTINUE
  28. >     IF (.NOT. SUCCESS) THEN
  29. >         IF (I .LT. 459) THEN
  30. >         I = I + 1
  31. >         IF (STATE .EQ. KNOWNSTATE(I)) THEN
  32. >             CALL {the Ith function, we have no syntax for this}
  33. >             SUCCESS = .TRUE.
  34. >         ENDIF
  35. >         GOTO 100
  36. >         ELSE
  37. >         CALL REPORT_UNKNOWN_STATE(STATE)
  38. >         ENDIF
  39. >     ENDIF
  40. > Furthermore, with some kind of array of pointers to functions, I could
  41. > exchange one function in the array for another during the execution of
  42. > the program.  Basically, the user pushes some button, and the program
  43. > starts behaving differently because it is calling different functions
  44. > in response to the user's input.
  45.  
  46.     Well, you _can_ do this with VAX Fortran ("Gurgle!"...to quote an earlier,
  47. unrelated posting... ;-).
  48.  
  49.     Turns out that in about 1981 when the LBL side of our collaboration
  50. starting writing the analysis code for my thesis experiment, they said, 
  51. "Hey! We don't have to use those _ugly_ DUMMY subroutines, we can just 
  52. store that addresses of the subroutines the user actually wants called in 
  53. an array, and pass the array value to a subroutine which calls the subroutine!
  54. So Eleagant!"  (So obscure to the uninitiated...but flexible...)
  55.  
  56.     It works in two steps:  the user writes an INIT subroutine which has
  57. a bunch of statements like (structure is simplified here):
  58.  
  59.     SUB_ARRAY(NEXT) = %LOC(THE_NEXT_SUBROUTINE_I_WANT_CALLED)
  60.     NEXT = NEXT+1
  61.  
  62. The %LOC built-in stored the address of THE_NEXT_SUBROUTINE_I_WANT_CALLED
  63. in array SUB_ARRAY.  Then from the main program there was a loop something
  64. like:
  65.  
  66.     DO I=1,LAST_SUB
  67.         CALL DO_SUB(%VAL(SUB_ARRAY(I))
  68.     ENDDO
  69.  
  70. in which the %VAL built-in passed the _value_ stored in SUB_ARRAY(I) to
  71. DO_SUB (rather than the address of SUB_ARRAY(I) ), and DO_SUB was something
  72. like:
  73.     SUBROUTINE DO_SUB(SUBR)
  74.     CALL SUBR()
  75.     RETURN
  76.     END
  77.  
  78.     Sorry to be so long-winded here, but I just couldn't resist giving an
  79. example that appears to match exactly what Steve is trying to do ;-)   In
  80. fact, I'd like to here from some of the compiler experts out there why
  81. something like this couldn't easily (relatively speaking) be made standard
  82. given that it has been possible to pass functions to subprograms for a long
  83. time?  Is it just committee politics?
  84.  
  85.     Cheers, Ken
  86. -- 
  87.  Dr. Kenneth H. Fairfield    |  Internet: Fairfield@Slacvx.Slac.Stanford.Edu
  88.  SLAC, P.O.Box 4349, MS 98   |  DECnet:   45537::FAIRFIELD (45537=SLACVX)
  89.  Stanford, CA   94309        |  BITNET    Fairfield@Slacvx
  90.  ----------------------------------------------------------------------------
  91.  These opinions are mine, not SLAC's, Stanford's, nor the DOE's...
  92.