Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!necntc!ames!amdahl!nsc!taux01!avi From: avi@taux01.UUCP (Avi Bloch) Newsgroups: comp.lang.modula2 Subject: Re: Procedure Types & Variables Message-ID: <345@taux01.UUCP> Date: Thu, 1-Oct-87 12:00:56 EDT Article-I.D.: taux01.345 Posted: Thu Oct 1 12:00:56 1987 Date-Received: Mon, 5-Oct-87 05:10:20 EDT References: Reply-To: avi@taux01.UUCP (Avi Bloch) Distribution: world Organization: National Semiconductor (Israel) Ltd. Lines: 60 In article Info-Modula2 Distribution List writes: >Modula allows a variable to be a procedure type, and that variable to >be assigned the "value" of different procedures. This is a very usefull >thing when making programs such as operating systems or I/O drivers, but >it leads to one question. Consider the following scrap of code: > > > PROCEDURE A ; > VAR > P : PROC ; > PROCEDURE B ; > VAR > X : CARDINAL ; > PROCEDURE C ; > BEGIN > X := 17 ; > END C ; > BEGIN > P := C ; > END ; > BEGIN > B ; > P ; > END A ; > > >Now, I realize this wouldn't compile because I forgot to say END B, but >once that was finished, what would you expect to happen at run time? >When procedure C assigns the value 17 to X, X does not exist because the >activation record for B does not exist. > Maybe this is why Wirth states emphatically (Programming in Modula-2 ed. 3, pp. 82): To conclude, we emphasize the restriction that procedures that are assigned to variables or are passed as parameters, must not be declared local to any other procedure. Therefore any procedure represented by a procedure variable will reference only local variables or global variables. Note that in Pascal, which only has procedure parameters, this problem does not exist, since that at the point where the procedure is passed as a parameter to a subroutine, if the procedure is accessable then all the activation records it needs must exist and will continue to exist at least until the return from the subroutine. Therefore the restriction Wirth mentions regarding Modula-2 does not exist in Pascal. (I purposely used different terms, i.e. procedure and subroutine to differentiate between the called procedure and the procedure passed as a parameter). Avi Bloch National Semiconductor (Israel) 6 Maskit st. P.O.B. 3007 Herzlia 46104, Israel (011-972) 52-522263 avi%taux01@nsc