As far as I know, when passing an array to a procedure / function, its address is passed, but not the array itself.

type TBigData = array of UInt64; procedure Add_pr(Var arr_to: TBigData; Const arr_from: TBigData); --суммирует два массива, результат пишет в первый begin arr_to[i] := arr_to[i] + arr_from[i];//Код пропущен, просто суммирование двух массивов в первый end; function Add_fu(Const arr_to, arr_from: TBigData): TBigData; --суммирует два массива, результат пишет в третий (fun.Result) begin Result[i] := arr_to[i] + arr_from[i];//Код пропущен, просто суммирование двух массивов в третий end; 

Sometimes you need to summarize into one of two arrays that are addends, sometimes you need to create a new one (the third one) and add a sum there.

The speed of the algorithm is important, the question is about the code, I want to not follow the two versions of the function procedure (there are the same "pairs" in division, multiplication, subtraction, etc., all for the same work inside two arrays, or to add the result to third).

How to combine the code percent / function without losing the speed of the algorithm? Sort of

 function Add_fu(Var arr_to: TBigData; Const arr_from: TBigData): TBigData; begin Result[i] := arr_to[i] + arr_from[i]; end; 

When calling, if you want to work create a third array:

 arr3 := Add_fu(arr1,arr2); 

If you need to add the sum to the first array of the item, the third array-result is not needed:

 arr1 := Add_fu(arr1,arr2); 

Is it possible to do without a little blood without losing the speed of the algorithm, with a minimum of changes. Will there be any problems with such a call ( arr1 := Add_fu(arr1,arr2) ) and creating copies and other brakes and errors? It seems that inside the Result function, arr_to is just a link to an address, and depending on the function call, they can be the same or different arrays, but who knows how this is implemented in Delphi 10.2?

  • What is meant by Delphi .Net ?? - MBo
  • Delphi 10.2, it’s like, on .Net 3.5, at least you have to download it when you install it - AntVa
  • @AntVa This is needed by the installer, not Delphi. - Igor
  • dotnet is needed by the installer and the development environment (it would be better to get rid of it), and the applications are native, so there is no need to associate .net with Delphi. - MBo
  • 2
    Do not get rid of. This has nothing to do with the compiler and the code. - MBo

2 answers 2

You can make and use it differently.

 procedure Add(A, B, C: TBigData); begin ... C[i] := A[i] + B[i]; end; Add(X, Y, Z); //сумму в третий Add(X, Y, X); //сумму в первый Add(X, X, X); //первый удвоить 
  • Why do we need var here? - Igor
  • To work not with copies - MBo
  • variables of dynamic arrays - pointers - Igor
  • Hmm, looking, I have already forgotten what it was about and what type it was. - MBo
 function Add(arr_to: TBigData; arr_from: TBigData; aCreateResult: boolean): TBigData; begin if aCreateResult then SetLength(result, Length(arr_to)); else result := arr_to; ... result[i] := arr_to[i] + arr_from[i]; end; 

I will pretend not to notice "Delphi.NET". If I'm not mistaken, the latest version of Delphi with the dotnet version was Delphi 2006.

  • Why not install Delphi 10.2 without .Net? - AntVa