Newsgroups: comp.lang.pascal Path: utzoo!utgpu!watserv1!watmath!dmurdoch From: dmurdoch@watmath.waterloo.edu (Duncan Murdoch) Subject: Re: Pointers to procedures Message-ID: <1991Apr8.222045.8757@watmath.waterloo.edu> Organization: University of Waterloo References: <26486@adm.brl.mil> Date: Mon, 8 Apr 1991 22:20:45 GMT Lines: 54 In article <26486@adm.brl.mil> 209507097@ucis.vill.edu (MCREE, JAMES F) writes: > > I been following the thread of calling procedures by pointers with a >lot of interest recently. Mostly because I have an application that is just >begging for a solution like that. However, the discussion and samples that I >have seen (including those in the manual) deal with hardcoding procedure names >into the program. (ie. Proc[i] := Do_It_Procedure) Is there a way of doing >what my syntactically incorrect program below is trying to do? ... >Write ('Enter the name of a procedure to run: '); >Readln (Proc_Name); >Proc_Name; >. >. >End. > > >This is an example of a user-specified call to a procedure. The user >specifies the procedure directly though. Can this be done? The closest thing to what you've got there would be to create an array of procedure variables, and a corresponding array of names. Then when the user enters a name, search through the name array for a match, and call the corresponding procedure. For example: var procs : array[1..2] of procedure; names : array[1..2] of string[8]; name : string[8]; i : integer; begin { In TP 6, these assignments could all be done in a Const declaration } procs[1] := add; names[1] := 'ADD'; procs[2] := subtract; names[2] := 'SUBTRACT'; readln(name); i := 1; while (i <= 2) and (names[i] <> name) do inc(i); if i <= 2 then procs[i]; end; I hope this helps. Duncan Murdoch dmurdoch@watstat.waterloo.edu (when it gets fixed) dmurdoch@watmath.waterloo.edu (temporarily)