Board index » delphi » Easy question: passing variables FROM a procedure

Easy question: passing variables FROM a procedure

Hi there! (I hope I spelled the titel correctly)

I have a problem. I have an array, which will contain graphic data.
I made a procedure that loads graphic data from a file into the array.
But it won't work! Here's an easy example:

{Start program}
VAR V: Array[1..4] of integer;

Procedure Try(V: array of integer);

begin;
V[2] := 4;
V[3] := 3;
end;

{Start actual program}
Begin;
try(V);
Writeln (V[2], ' - ', V[3]);
end.

I know in basic, you can SHARE a variable. I do not know how to do this is
Pascal. The result of this program is: "0 - 0", and I did really put 4 and
3 in the variable.

Can anyone help me? And could you mail the answer also to me?
Thanks

Jeroen van der Heide
L...@WORLDACCESS.NL

 

Re:Easy question: passing variables FROM a procedure


lenk wrote in a message to All:

 > I have a problem. I have an array, which will contain graphic data. I
 > made a procedure that loads graphic data from a file into the array.
 > But it won't work! Here's an easy example:

Change it to:

Type
  V_Array : Array[1..4] of Integer;

Var
  V : V_Array;

Procedure Try(Var Vt : V_Array);
              ^^^  
Begin
  Vt[2] := 4;
  Vt[3] := 3;
End;

Begin;
  Try(V);
  Writeln (V[2], ' - ', V[3]);
End.

 > I know in basic, you can SHARE a variable. I do not know how to do
 > this is  Pascal. The result of this program is: "0 - 0", and I did
 > really put 4 and  3 in the variable.

You can share it. If V is a global variable, you can.
Just use:

Procedure Try;

  Begin
    V[1] := 2;
    V[2] := 3;
  End;

Groeten,
         Albert            

--------------------------------------------------------------------
Techlink Datacommunications
Internet: albe...@worldaccess.nl   FidoNet 2:283/400   Doetinchem NL

Newsgroups: wxs.os2
Path: albertd
Subject: All
Date: 04 Feb 97  19:57:06
Message-ID: <04002832.2f7db7f0@worldaccess.nl>
Reply-To: albe...@worldaccess.nl (Albert Dorrepaal)
Organization: Techlink BBS, Doetinchem NL
X-Mailer: Ftnsoup v1.008

Leo Van.Zantvoort wrote in a message to All:

 AD> Zijn hier nog OS/2'ers aanwezig?
 > Potverdrie Albert... Dat nou net JIJ dat moet vragen :-)
 > Ik zit ook al te kijken... Zijn wij de enige zeker... Nou, mooi geen
 > last van ingesprek, hangende news servers of verdwijnende Email...

Ik begrijp ook niet waarom men zo klaagt. Nooit in gesprek. Mail en News servers draaien prima...

 > alleen ff kijken of ISDN zostraks met OS/2 aan de gang te krijgen
 > is...

Vast wel. Er is zelfs een speciale dialer voor. Staat wel ergens op het bbs.

Groeten,
         Albert            

--------------------------------------------------------------------
Techlink Datacommunications
Internet: albe...@worldaccess.nl   FidoNet 2:283/400   Doetinchem NL

Newsgroups: wxs.os2
Path: albertd
Subject: All
Date: 04 Feb 97  19:59:58
Message-ID: <04002832.2f7db...@worldaccess.nl>
Reply-To: albe...@worldaccess.nl (Albert Dorrepaal)
Organization: Techlink BBS, Doetinchem NL
X-Mailer: Ftnsoup v1.008

Sebastiaan Ubink wrote in a message to All:

 > Yep.  Ik heb alleen niet zoveel te melden want alles werkt okay
 > (stukken beter dan Warp 3.0). Alleen Framemaker 4.0 heeft wat  moeite
 > met de fonts onder WinOs/2, dus toch maar even afwachten  tot de
 > eerste fixpack er is.

Ik krijg Windows internet software nog niet aan de gang onder WinOs/2.
Ih heb de directory waar WINSOCK.DLL in staat, in het pad staan maar het
werkt toch nog niet. Iemand tips?

Groeten,
         Albert            

--------------------------------------------------------------------
Techlink Datacommunications
Internet: albe...@worldaccess.nl   FidoNet 2:283/400   Doetinchem NL

Re:Easy question: passing variables FROM a procedure


In article <N.020497.190018.81@ISDN> of Tue, 4 Feb 1997 18:00:18 in

Quote
comp.lang.pascal.borland, l...@worldaccess.nl wrote:
>I have a problem. I have an array, which will contain graphic data.
>I made a procedure that loads graphic data from a file into the array.
>But it won't work! Here's an easy example:

>{Start program}
>VAR V: Array[1..4] of integer;

>Procedure Try(V: array of integer);

>begin;
>V[2] := 4;
>V[3] := 3;
>end;
> ...

Seems you need    Try(var V : array ...
                  ^^^

That will help, but will I think not give what you want.

        type A4 = array [1..4] of integer ;
        var V : A4 ;
        procedure Try(var V : A4) ;
may suit you.

Web URL: http://www.merlyn.demon.co.uk/pascal.htm#ProcPars  may help
you.
--
John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk  Turnpike v1.12  MIME
  Web URL: http://www.merlyn.demon.co.uk/
  Standard signature separator is as above, a line containing "-- " (SoRFC1036)

Re:Easy question: passing variables FROM a procedure


On Tue, 4 Feb 1997 l...@worldaccess.nl wrote:

Quote
> Hi there! (I hope I spelled the titel correctly)

> I have a problem. I have an array, which will contain graphic data.
> I made a procedure that loads graphic data from a file into the array.
> But it won't work! Here's an easy example:

> {Start program}
> VAR V: Array[1..4] of integer;

> Procedure Try(V: array of integer);

> begin;
> V[2] := 4;
> V[3] := 3;
> end;

> {Start actual program}
> Begin;
> try(V);
> Writeln (V[2], ' - ', V[3]);
> end.

> I know in basic, you can SHARE a variable. I do not know how to do this is
> Pascal. The result of this program is: "0 - 0", and I did really put 4 and
> 3 in the variable.

> Can anyone help me? And could you mail the answer also to me?
> Thanks

> Jeroen van der Heide
> L...@WORLDACCESS.NL

Try declaring the procedure as:

procedure try(var v:array of integer);

Daniel Mantione

Re:Easy question: passing variables FROM a procedure


Quote
l...@worldaccess.nl wrote:
> I have a problem. I have an array, which will contain graphic data.
> I made a procedure that loads graphic data from a file into the array.
> But it won't work! Here's an easy example:

> {Start program}
> VAR V: Array[1..4] of integer;

> Procedure Try(V: array of integer);[snipped, not relevant to problem]

For this kind of thing to work under pascal (changing the contents of a
variable passed to a procedure in such a way that caller can use the
modifications), you have to declare the formal parameter as a VAR parameter,
in your case:

PROCEDURE Try( VAR V : ARRAY OF Integer);

The keyword VAR in this declaration results in the passing of a pointer
to the variable, if you leave it out, a copy is passed on stack.

So, the use of 'VAR' in a procedure declaration has to effects (related):
1- modifications of the parameter's value will be returned to caller; and
2- less stack space is used: only a pointer is passed, not the whole variable
    (array in this case);

Veel succes,

Remco

--
--------------------------------------------------------------------------------
This is an automatically generated signature for Remco Vietor in netscape.
The views expressed in this message should be taken as the personal views of
the author, unless stated otherwise.

Other Threads