ni-
duisburg.de!sun1.hrz.uni-essen.de!aixrs1.hrz.uni-essen.de!phy580
Quote
>From: phy...@aixrs1.hrz.uni-essen.de (Frank Fetthauer)
>Newsgroups: comp.lang.pascal.borland
>Subject: BP7: Function of type function
>Date: 8 Aug 1995 14:59:04 GMT
>Organization: Universitaet Essen GH, Germany
>Lines: 59
>Message-ID: <407u3o$...@sun1.uni-essen.de>
>NNTP-Posting-Host: aixrs1.hrz.uni-essen.de
>X-Newsreader: TIN [version 1.2 PL2]
>Status: N
>I am writing a routine which should read a string like ' Sin(x)*x+x ' and
>calculate the result. It works already, but the string is analysed
>each time x is changed. So the routine is slow. I thought I should write
>a preprocessor which analyses the string once and build a nested function
>hierachy.
> *** deleted section ***
>function analyse(ch:char):fun; <------- Compiler will not compile this line
(*
Passing of procedural variables can be tricky, but if you study
it a while, it will all make sense (or you'll stop worrying about
how it works, just that it works).
Turbo Pascal won't/can't return typed procedures on the stack via
a function, but it can return the procedure's address as a pointer
(which is what you really want).
A couple of general guidelines first...
.. . . . .. . . .. . . .. . . .. . . .. . . .. . .
* Be sure to declare any procedures that you pass arround like
this as far or enable the {$F+} compiler directive.
.. . . . .. . . .. . . .. . . .. . . .. . . .. . .
* You should also have a typed pointer of these procedure/function types,
or you must type cast the pointers when referenced.
type
fun = function(x,y:real):real;
pfun = ^fun;
.. . . . .. . . .. . . .. . . .. . . .. . . .. . .
* Use the @ operator the retrieve the address of procedures you will pass.
When you dereference a pointer of type pfun in a variable of type fun,
use the @parameter of the LEFT side of the assignment - I love this
stuff!.
var
a : fun;
b : pfun
begin
@a := b;
end;
.. . . . .. . . .. . . .. . . .. . . .. . . .. . .
I hope that was somewhat understandable. If not, try the code below.
*)
program test;
type
fun = function(x,y:real):real;
pfun = ^fun; { pointer to a fun function }
function test1(x,y:real):real; far;
begin
test1:=x+y;
end;
function test2(x,y:real):real; far;
begin
test2:=x-y;
end;
function analyse(ch:char): pfun; { <------- Compiler *WILL* compile this line
begin
case ch of
'+': analyse:=@test1;
'-': analyse:=@test2;
end;
end;
var
a : fun;
begin
@a := analyse('+'); { @ assignment on LEFT side }
writeln(a(3,4););
@a := analyse('-');
writeln(a(13,4));
end.
--------------------------------------------------------------------------
. .
.. ..
... ... N O R T H W E S T C O M P U T E R S E R V I C E S
..... email: nwc...@thiefriverfalls.polaristel.net
... ...
.. .. Commercial Programming Services/Support
. .