Passing array by value - surprising result lazarus / 62-General ---------------------------------------------------------------------- j831526 · 2019-06-09 18:38 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PROGRAM p6;{$H-}USES  sysutils; TYPE  pass_test = Array[1..4] of Integer; VAR  ta : pass_test;  i : Integer;//===================PROCEDURE pbv(x : pass_test);VAR  i : Integer;BEGIN  for i := 1 to 4 DO    x[i] := 2 * i;  Writeln('End of pbv: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);END;//====================================PROCEDURE pbr(var x : pass_test);VAR  i : Integer;BEGIN  for i := 1 to 4 DO    x[i] := 2 * i;  Writeln('End of pbr: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);END;//===== Main ====BEGIN  Writeln('Start p_test');  for i := 1 to 4 DO    ta[i] := i;  Writeln('Initial array: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);  pbv(ta);  Writeln('After pbv: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);  pbr(ta);  Writeln('After pbr: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]);END./code] Output is:-----------Start p_testInitial array: 1 2 3 4End of pbv: 1 2 3 4After pbv: 1 2 3 4End of pbr: 2 4 6 8After pbr: 2 4 6 8 I expected End of pbv to be 2 4 6 8  Any suggies? Thanx ... Charlie ---------------------------------------------------------------------- Jonas Maebe · 2019-06-09 18:48 Quote from: j831526 on June 09, 2019, 08:38:01 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PROCEDURE pbv(x : pass_test);VAR  i : Integer;BEGIN  for i := 1 to 4 DO    x[i] := 2 * i;  Writeln('End of pbv: ', ta[1], ' ', ta[2], ' ', ta[3], ' ', ta[4]); You are writing "ta" instead of "x" here. ---------------------------------------------------------------------- ASerge · 2019-06-09 19:08 Quote from: Jonas Maebe on June 09, 2019, 08:48:44 pmYou 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 · 2019-06-09 19:36 Arggg - brain fart:( Sorry for bothering everyone. The correct code gives the expected result. Thanx for the replies ... Charlie