Recent

Author Topic: Passing array by value - surprising result  (Read 973 times)

j831526

  • Newbie
  • Posts: 4
Passing array by value - surprising result
« on: June 09, 2019, 08:38:01 pm »
I ran a little test of passing array by value and reference. Normally, variables passed by value can be used like local variables in the called function or procedure. They can be modified within the called code without changing the value in the caller's code. Seems right.

But this doesn't seem to work the same for arrays. The compiler lets me modify the array in the called code but does NOT actually do the modification! No error or warnings are generated.
Code: Pascal  [Select][+][-]
  1. PROGRAM p6;
  2. {$H-}
  3. USES
  4.   sysutils;
  5.  
  6. TYPE
  7.   pass_test = Array[1..4] of Integer;
  8.  
  9. VAR
  10.   ta : pass_test;
  11.   i : Integer;
  12. //===================
  13. PROCEDURE pbv(x : pass_test);
  14. VAR
  15.   i : Integer;
  16. BEGIN
  17.   for i := 1 to 4 DO
  18.     x[i] := 2 * i;
  19.   Writeln('End of pbv: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);
  20. END;
  21. //====================================
  22. PROCEDURE pbr(var x : pass_test);
  23. VAR
  24.   i : Integer;
  25. BEGIN
  26.   for i := 1 to 4 DO
  27.     x[i] := 2 * i;
  28.   Writeln('End of pbr: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);
  29. END;
  30. //===== Main ====
  31. BEGIN
  32.   Writeln('Start p_test');
  33.   for i := 1 to 4 DO
  34.     ta[i] := i;
  35.   Writeln('Initial array: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);
  36.   pbv(ta);
  37.   Writeln('After pbv: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);
  38.   pbr(ta);
  39.   Writeln('After pbr: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);
  40. END.
  41. /code]
  42.  
  43. Output is:
  44. -----------Start p_test
  45. Initial array: 1 2 3 4
  46. End of pbv: 1 2 3 4
  47. After pbv: 1 2 3 4
  48. End of pbr: 2 4 6 8
  49. After pbr: 2 4 6 8
  50.  
  51. I expected End of pbv to be 2 4 6 8
  52.  
  53. Any suggies?
  54.  
  55. Thanx ... Charlie

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1059
Re: Passing array by value - surprising result
« Reply #1 on: June 09, 2019, 08:48:44 pm »
Code: Pascal  [Select][+][-]
  1. PROCEDURE pbv(x : pass_test);
  2. VAR
  3.   i : Integer;
  4. BEGIN
  5.   for i := 1 to 4 DO
  6.     x[i] := 2 * i;
  7.   Writeln('End of pbv: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);
  8.  
You are writing "ta" instead of "x" here.

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: Passing array by value - surprising result
« Reply #2 on: June 09, 2019, 09:08:00 pm »
You are writing "ta" instead of "x" here.
An example of why global variables should not be used. In this case, declaring them at the end, or better yet extracting the code between "begin" and "end." into a separate procedure, would allow the compiler to immediately show the error.

j831526

  • Newbie
  • Posts: 4
Re: Passing array by value - surprising result
« Reply #3 on: June 09, 2019, 09:36:40 pm »
Arggg - brain fart:( Sorry for bothering everyone. The correct code gives the expected result.
Thanx for the replies ... Charlie

 

TinyPortal © 2005-2018