[comp.lang.pascal] passing two variables

tpehrson@javelin.sim.es.com (Tim Clinkenpeel) (06/21/91)

i would like a function to pass back two variables.  is this possible?  
below is an example of what i'd like to accomplish:

cx,cy :=checkvalid(cx+offset,cy+offset);

-- 
/=============================/ "1:1 And, yea, Jesus was not quite dead and did
/ tpehrson@javelin.sim.es.com /  commeth down with a wailing and a gnashing of
/=====aka: tim clinkenpeel====/  teeth" _The_Bible_][:_Jesus_Strikes_Back_ 
/ [DISCLAIMER: i take prozac] / WARNING: my messages are offensive to morons

smith@ctron.com (Larry Smith) (06/21/91)

In article <1991Jun21.144356.19598@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
>i would like a function to pass back two variables.  is this possible?  
>below is an example of what i'd like to accomplish:
>
>cx,cy :=checkvalid(cx+offset,cy+offset);

In a word: no.

Some Pascals let you return a record, you could try:

 TYPE
   RtnVal = RECORD
     cx, cy: WHATEVER;
   END;

  FUNCTION checkvalid(x,y: WHATEVER) : RtnVal;

  VAR
    r: RtnVal;
  BEGIN
    r := checkvalid(x,y);
  END.

Of course, some Pascals don't.  In which case you will need:

  FUNCTION checkvalid(x,y: WHATEVER): ^RtnVal;

  and

  VAR
    r: ^RtnVal;

 Not elegant, but it works.


-- 

Larry Smith
smith@ctron.com
The usual disclaimer stuff...

elmo@troi.cc.rochester.edu (Eric Cabot) (06/22/91)

In <1991Jun21.144356.19598@javelin.sim.es.com> tpehrson@javelin.sim.es.com (Tim Clinkenpeel) writes:

>i would like a function to pass back two variables.  is this possible?  
>below is an example of what i'd like to accomplish:

>cx,cy :=checkvalid(cx+offset,cy+offset);


Well one approach would be to combine the two variables as one
and then separate them after the function returns.
For example if cx and cy are bytes then you could have     
 cxcy defined as a word and just use Hi(cxcy) and Lo(cyxy)
to get them. If cx and cy are words (or integers) then you
could put them together as a longint and  break that up.

I hope this helps.

-EC

--
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Eric Cabot                              |    elmo@uhura.cc.rochester.edu
      "insert your face here"           |    elmo@uordbv.bitnet
=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

dmurdoch@watstat.waterloo.edu (Duncan Murdoch) (06/22/91)

In article <1991Jun21.144356.19598@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
>i would like a function to pass back two variables.  is this possible?  
>below is an example of what i'd like to accomplish:
>
>cx,cy :=checkvalid(cx+offset,cy+offset);

Not in Turbo or Standard Pascal.  The easiest way to do it is to add variables
to receive the results as var parameters, which your function (or it could
be a procedure) can modify.  You have to be a little careful of aliasing,
e.g. a declaration

  procedure checkvalid(var in1,in2:integer;var out1,out2:integer);

would likely get messed up on the call

  checkvalid(cx,cy,cx,cy);

since in1 and out1 would both refer to cx, and in2 and out2 would both refer to
cy.  Passing the input parameters by value (not using var) should work.

Duncan Murdoch

gaertner@tertius.in-berlin.de (06/25/91)

In article <1991Jun21.175817.339@maytag.waterloo.edu>, dmurdoch@watstat.waterloo.edu (Duncan Murdoch) writes:
> In article <1991Jun21.144356.19598@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
>>i would like a function to pass back two variables.  is this possible?  
>>below is an example of what i'd like to accomplish:
>>
>>cx,cy :=checkvalid(cx+offset,cy+offset);
> 
> Not in Turbo or Standard Pascal.  The easiest way to do it is to add variables
> to receive the results as var parameters, which your function (or it could
> be a procedure) can modify.  You have to be a little careful of aliasing,
> e.g. a declaration
> 
>   procedure checkvalid(var in1,in2:integer;var out1,out2:integer);
> 
> would likely get messed up on the call
> 
>   checkvalid(cx,cy,cx,cy);
> 
> since in1 and out1 would both refer to cx, and in2 and out2 would both refer to
> cy.  Passing the input parameters by value (not using var) should work.
> 
> Duncan Murdoch

A clearer solution would be

  PROCEDURE checkvalid ( offset1, offset2 : INTEGER ; { pure input }
                         VAR inout1, inout2 : INTEGER { input/output} ) ;

with the call

  checkvalid ( offset1, offset2, cx, cy ) ;

Refering to the original question:
   Pascal exspects a variable on the left side of an assigment, so there
   will never be a construct like
      x,y := expression ;
In Algol 60 (and I think in C) it is possible to have a list of variables
on the left side of an expression, but note that these variables would get
the same value and not different values.

Ralf

--

  Ralf Gaertner              gaertner@venus.rz-berlin.mpg.de
  FHI Berlin

defaria@hpcupt3.cup.hp.com (Andy DeFaria) (06/25/91)

>/ hpcupt3:comp.lang.pascal / tpehrson@javelin.sim.es.com (Tim Clinkenpeel) /  7:43 am  Jun 21, 1991 /
>i would like a function to pass back two variables.  is this possible?  
>below is an example of what i'd like to accomplish:
>
>cx,cy :=checkvalid(cx+offset,cy+offset);

I'll take  a stab here.  Assuming  that the function  name checkvalid is to
indeed check if something is valid then why not:

function checkvalid (var parm1 : integer; 
                     var parm2 : integer) : boolean;

Then you could:

	if not checkvalid (foo, bar) then
	   begin
	   writeln ('Error has occured');
	   halt
	   end;
	else
	   { Other processing }

This allows you to return a result specifying whether things are OK as well
as modify the parameters.   Ah I  see where  your probably having problems!
You  trying to  pass an  expression   and the  compiler doesn't   allow var
parameters  to be  expressions  - it want  a bonified  variable that it can
modify.  You'll need to:

var
   cx    : integer;
   cy    : integer;
   cxnew : integer;
   cynew : integer;

begin
   ...

   cxnew := cx + offset;
   cynew := cy + offset;

   if checkvalid (cxnew, cynew) then
      { process }
   else
      writeln ('Error');

Russell_Mennie@resbbs.UUCP (Russell Mennie) (06/26/91)

Your other option... would be to use pointers.  and have the first pointer

var
    p  :Pointer;
    r    :Byte;



point to the byte variable...

cslaurie@cybaswan.UUCP (Laurie Moseley ) (06/28/91)

---------------------------------------------------------------------------

If you want to return 2 variables from a function (or 42 for that
matter) the easy way to do it is to return a pointer to a linked list
of the input data items. That pointer can then be passed to the block
which does the processing, and it can process as many as you wish.
It even handles null input rather elegantly.

I don't know who the original poster was, but if they would like a
copy of a program which does this, email me.


Laurie

(apologies to the referee- original article number not known)
==========================================================================

Laurie Moseley

Computer Science, University College, Swansea SA2 8PP, UK

Tel: +44 792 295399

JANET:	cslaurie@uk.ac.swan.pyr

=========================================================================
"Why does the Turing test set such abysmally low standards ?"
===========================================================================

hhallika@zeus.calpoly.edu (Harold Hallikainen) (06/29/91)

	Ideally, I'd hope we could have a function result type
be an array, allowing you to return any specified number of
results.  I believe you can do this if you want to mess with 
things a bit.  Since a string is the same as a character or
byte array, and we are allowed to return strings, declare the
function result type as a string.  If an array of bytes is a
useful result, you can refer to the results directly.  If you
want some other type of results (maybe even real), seems like
something with variant records could be done.  Describe the
result as a character array and as an array of reals, then
refer to the real names.  
	I haven't tried this, but it seems logical (which is
probably dangerous!).
	Good luck!

Harold

--
Harold Hallikainen              ap621@Cleveland.Freenet.edu
Hallikainen & Friends, Inc.     hhallika@pan.calpoly.edu
141 Suburban Road, Bldg E4      phone 805 541 0200 fax 544 6715
San Luis Obispo, CA 93401-7590  telex 4932775 HFI UI