passing whole arrays as parameters 
Author Message
 passing whole arrays as parameters

Hi guyz,

Is there a way to pass arrays to a procedure as parameters (variable
and by reference)?? If yes, how??

Rgds,
Ivan



Wed, 18 Jun 1902 08:00:00 GMT  
 passing whole arrays as parameters

Quote:

>Hi guyz,
>Is there a way to pass arrays to a procedure as parameters (variable
>and by reference)?? If yes, how??
>Rgds,
>Ivan

Although this was help to someone else wanting to pass an array to a
function in standard Pascal, I think it will help.

This is written in Turbo v.6.0. The key thing in passing
parameters to both Functions and Procedures is that the data passed
be the same type as declared in the Function or Procedure, even if
you have to make a new global type as I did here. Although the disk
reading is simulated and the program is "hoaky", it runs on my PC
and I hope your computer.

Regards,
Clif   2:20PM  6/15/96

Program ArrayToFunction;

TYPE
ShortString = Array[1..3] of Char;
FourElements = Array[1..4] of ShortString;

VAR
FromDisk:Array[1..12] of ShortString;
CopyFour:FourElements;
n, m, FuncAns:Integer;

Function PrintFour(FourParms:FourElements):Integer;
(* prints out the four elements and returns an integer *)
VAR ArryIndx, ChIndx:Integer;

Begin
     For ArryIndx := 1 to 4 do
     Begin
         Write(ArryIndx, '--');
         For ChIndx := 1 to 3 do
         Begin
              Write(FourParms[ArryIndx][ChIndx]);  (* a char *)
         End;
         Writeln;
     End;
     (* extracting an integer for test purposes *)
     PrintFour := ord(FourParms[1][1]) - ord('0');
End;

Begin   (* main *)
(* loading a 12 element array with known values *)

FromDisk[1] := '111';  FromDisk[2] := '222';  FromDisk[3] := '333';
FromDisk[4] := '444';   FromDisk[5] := '555';  FromDisk[6] := '666';
FromDisk[7] := '777';  FromDisk[8] := '888';  FromDisk[9] := '999';
FromDisk[10] :='100' ; FromDisk[11] := '111'; FromDisk[12] := '122';

n := 0;
While n < 12 do
Begin
     For m := 1 to 4 do
     Begin
          n := n + 1;
          CopyFour[m] := FromDisk[n];
     End;
     FuncAns := PrintFour(CopyFour);   (* <---- NOTE *)
     Write('** Pass ', n div 4, ' -- ');
     Writeln('Twice first element char val = ', 2 * FuncAns);
     Writeln;
End;
(* check on original array contents *)
Writeln('*** Contents of original array: ');
For n := 1 to 12 do
Begin
    Write(FromDisk[n], '  ');
    If n mod 4 = 0 then Writeln;
End;
Writeln;
Writeln('Program ends--press enter') ;
Readln;
End.



Wed, 18 Jun 1902 08:00:00 GMT  
 passing whole arrays as parameters

No problem :

Type
  TMyArray = Array[1..30] Of SomeThing;

Procedure DoStuff(Var AnArray : TMyArray);
Begin
  {whatever}
End;

Some facts:

Use the Var ketyword (by reference calling). Otherwise you're complete
array is placed on stack. Not wanted! This is just one way of doing it,
other methods are using pointers or untyped parameters. But that's for you
to find out.

Cheers,
Anco.



Wed, 18 Jun 1902 08:00:00 GMT  
 passing whole arrays as parameters


Quote:
> Hi guyz,
> Is there a way to pass arrays to a procedure as parameters (variable
> and by reference)?? If yes, how??

  Of course, for example:

procedure A(var AnArray: array of AType);
begin
{...}
end;

Quote:
> Rgds,
> Ivan

--
*********************************************

*********************************************


Wed, 18 Jun 1902 08:00:00 GMT  
 passing whole arrays as parameters

Quote:
> Is there a way to pass arrays to a procedure as parameters (variable
> and by reference)?? If yes, how??

   Sure, declare it as a type, and pass a (var) parameter of that type.  
Of course, why do all that when you can quite reasonably use the array
globally?  (Unless you have several such arrays declared and must process
each of them in a common routine...8<}})


Wed, 18 Jun 1902 08:00:00 GMT  
 passing whole arrays as parameters


Quote:
>Type
>  TMyArray = Array[1..30] Of SomeThing;
>Procedure DoStuff(Var AnArray : TMyArray);
>Begin
>  {whatever}
>End;
>Some facts:
>Use the Var ketyword (by reference calling). Otherwise you're complete
>array is placed on stack. Not wanted! This is just one way of doing it,
>other methods are using pointers or untyped parameters. But that's for you
>to find out.
>Cheers,
>Anco.

Well, that does (somewhat) depend on the size of something.  Putting, say 60
bytes on the stack is not a big deal.  If you're not planning to change the
values of the array, you can also pass it as a const parameter, ie

Procedure DoStuff(const AnArray : TMyArray);

That'll only pass the pointer as well but do some checks that you don't
inadvertantly change the array within the procedure.

Hope this helps,
Jochen



Wed, 18 Jun 1902 08:00:00 GMT  
 passing whole arrays as parameters

Quote:

>Is there a way to pass arrays to a procedure as parameters (variable
>and by reference)?? If yes, how??

This is possible if you use a new type for the array:

program testprg;
type testarray=array[1..10] of byte;
var testvar: testarray;

procedure testproc(v: testarray);
 begin
  writeln(v[5]);
 end;

begin
 testproc(testvar)
end.

You can use procedure testproc(var v: testarray); to get the array
passed by reference.

Yours, Michael



Wed, 18 Jun 1902 08:00:00 GMT  
 passing whole arrays as parameters

: Hi guyz,
:
: Is there a way to pass arrays to a procedure as parameters (variable
: and by reference)?? If yes, how??
:
: Rgds,
: Ivan

This sounds that simple that I dont know wether I caught the point!
Oh, well, you must define an array type first!
(Working with variable length arrays is a different story... well, not that
different, but you would not be able to pass it per value);

So it is:
--------------------------------
TYPE
   pAnArray = AnArray;
   tAnArray = ARRAY [0..1024] OF BYTE {or whatever you like}

{ _DONT_ use this one! As the whole array will be copied to the stack it's
  da... slow!
  BTW: Your stack may easily overflow that way }
PROCEDURE UseAnArray1 (anArray : tAnArray);
BEGIN
   ...
END;

PROCEDURE UseAnArray2 (VAR anArray : tAnArray);
BEGIN
   ...
END;

PROCEDURE UseAnArray3 (anArrayPtr : pAnArray);
BEGIN
   ...
END;

VAR
   anArray : tArray;
   anotherArray : pArray;
BEGIN
   ...
   UseAnArray1 (anArray);
   UseAnArray1 (anotherArray^);
   UseAnArray2 (anArray);
   UseAnArray2 (anotherArray^);

   UseAnArray3 (anotherArray);
   ...
END;
----------------------------------

     CU Peter

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

     There was a point to this story, but it has temporarily escaped the
     chronicler's mind.
                   Douglas Adams, "So Long, and Thanks for all the Fish"



Wed, 18 Jun 1902 08:00:00 GMT  
 passing whole arrays as parameters

To pass the arrray as the parameter, you must declear using type first
ex.
  type
   ar=array[1..size] of celltype;
 var
  arra:ar;
then you could pass it to the preocedure.

Quote:

>Hi guyz,
>Is there a way to pass arrays to a procedure as parameters (variable
>and by reference)?? If yes, how??
>Rgds,
>Ivan



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Passing parameters to stored procedure.

2. Pass a TTable as Parameter to another object.

3. Passing Parameters to an Update Query

4. Parameter Query not passing Param

5. Passing TMemoField as a TStringList parameter to a procedure

6. Access 97 report from Delphi and passing parameters

7. passing SQL parameters

8. Passing functions as parameters in Turbo Pascal

9. Passing Sql parameters Help

10. Passing NULL parameters to a stored procedure

11. How to pass parameter to TQuery and retrieve value from TQuery

12. Passing indefinite number of parameters

 

 
Powered by phpBB® Forum Software