Path: utzoo!utgpu!water!watmath!clyde!bellcore!faline!thumper!ulysses!ucbvax!ucdavis!iris!windley From: windley@iris.ucdavis.edu (Phil Windley) Newsgroups: comp.lang.pascal Subject: Re: Procedures or Functions as parameters Keywords: Has anyone done it in TP4? Message-ID: <2165@ucdavis.ucdavis.edu> Date: 3 Jun 88 05:26:10 GMT References: <213@lafcol.UUCP> Sender: uucp@ucdavis.ucdavis.edu Reply-To: windley@iris.UUCP (Phil Windley) Organization: U.C. Davis - College of Engineering Lines: 67 In article <213@lafcol.UUCP> pilgrimk@lafcol.UUCP (Pilgrim Kenwyn A) writes: >Has anyone tried to pass procedures or functions as parameters within >user written procedures? Yes, see my example below. > For example, the write(ln) procedure can be >used with functions > > e.g. write(Centered(Message)) > -where Centered is a user-written function > with Message as a parameter > I don't understand your example. It seems that Centered(Message) will be evaluated and the result sent to write(), no magic here. From your first question I presume you want to do something like this: ------------ cut here -------------------------------- program sumtest(input, output); function sum(function term(x: integer): integer; a: integer; function next(y: integer): integer; b: integer):integer; begin if (a > b) then sum := 0 else sum := term(a) + sum(term, next(a), next, b); end; function sumCubes(a: integer; b: integer): integer; function cube(x:integer):integer; begin cube := x * x * x; end; function onePlus(x: integer): integer; begin onePlus := x + 1 end; begin sumCubes := sum(cube, a, onePlus, b); end; begin writeln(sumCubes(1,10)); end. --------------- end of example ------------------- Note that sum() is a function that takes two functions as parameters. We can define sumCubes() using sum(). (I have an example using sum() to do integration as well.) This program compiles on Berkeley pascal and as far as I know uses no non-standard Pascal constructs. Phil Windley | windley@iris.ucdavis.edu Robotics Research Lab | ucbvax!ucdavis!iris!windley University of California, Davis |