[comp.lang.pascal] function return value

MATHRICH@umcvmb.missouri.edu (Rich Winkel UMC Math Department) (03/08/89)

I'm probably just being nitpicky here, but I'm trying to optimize some
turbopas routines.  Here's what I'd like to do:
function what(var where:string):string;
begin
   what:=where;
   what:=what+what; (* string concatenation *)
end;
Obviously, in it's current form, any compiler will probably attempt to
deal with the concatenation statement as a recursive function call, but that's
not what I'm after.  I'd actually like to reference the function return
buffer (on the stack) as a variable.
I tried something like:
var alias:string absolute what;
in an attempt to reference the buffer, but TP 5 acts like it wants the 'what'
to be an actual function call, which doesn't make much sense, since it's in
a non executable statement.  Anyway, does anyone have any ideas?

Thanks,
Rich Winkel

jc58+@andrew.cmu.edu (Johnny J. Chin) (03/09/89)

Your function should read like this:

function what (x:string):string;
var tmp:string;
begin
  tmp:=x;
  what := tmp + tmp;
end;

In your original function, the line which reads:

  what:=what+what;

causes an error because "what" is a function and NOT a variable.

I hope this info helps.
      __________                                ___
     /          \                          /   /    /_/ / /\/
    _/  /   /   /                       __/.  /__  / / / / /
   /     /     /
  /           / 4730 Centre Ave. #412   ARPAnet: Johnny.J.Chin@andrew.cmu.edu
 /  -------  /  Pittsburgh, PA  15213   BITnet:  jc58@andrew
 \__________/   (412) 268-8936          UUCP: ...!harvard!andrew.cmu.edu!jc58
 Computer Dr.

Disclaimer:   The views expressed herein are STRICTLY my own, and not CMU's.

milne@ics.uci.edu (Alastair Milne) (03/10/89)

MATHRICH@umcvmb.missouri.edu (Rich Winkel UMC Math Department) writes:
>function what(var where:string):string;
>begin
>   what:=where;
>   what:=what+what; (* string concatenation *)
>end;
>I'd actually like to reference the function return buffer (on the stack) 
> as a variable.
>I tried something like:
>var alias:string absolute what;
>in an attempt to reference the buffer, but TP 5 acts like it wants the 'what'
>to be an actual function call

     Turbo Pascal has never been very clever in its error diagnoses.  It seems
     to be a little better now than it was in versions 2 or 3, but not much.

     Referring to the actual address of the function (which you could do with
     ADDR( What) ) is most unlikely to get you anywhere -- it's bound to be 
     the address of the code in memory, and not of the function result.  In
     fact, Turbo used to use AX to hold the result, and the means of getting
     to that are a) probably more trouble than convenience, and it's
     convenience you're after, and b) unreliable, since you can't be sure
     what's using AX at the time you reference it.

     You mention the stack.  Are you sure TP5 uses the stack for function
     results?  There are Pascals that do, but I doubt if Turbo is among them.
     Previous versions have not.

     I have seen at least one Pascal with a function-like construct like 
     "VALUE(<funcname>)", which always returned the function's present value, 
     rather than assuming a recursive call.  As far as I know, though, this is
     not standard at all.


     Alastair Milne

leonard@bucket.UUCP (Leonard Erickson) (03/14/89)

In article <18581@adm.BRL.MIL> MATHRICH@umcvmb.missouri.edu (Rich Winkel UMC Math Department) writes:
<I'm probably just being nitpicky here, but I'm trying to optimize some
<turbopas routines.  Here's what I'd like to do:
<function what(var where:string):string;
<begin
<   what:=where;
<   what:=what+what; (* string concatenation *)
<end;

Maybe I'm missing something here, but why not write it this way:
function what(var where:string):string;
begin
  what := where + where; (* string concatenation *)
end;

-- 
Leonard Erickson		...!tektronix!reed!percival!bucket!leonard
CIS: [70465,203]
"I'm all in favor of keeping dangerous weapons out of the hands of fools.
Let's start with typewriters." -- Solomon Short