Recent

Author Topic: Function returns with array  (Read 17735 times)

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Function returns with array
« on: April 02, 2018, 03:50:12 pm »
I found this code below:
Code: Pascal  [Select][+][-]
  1. function SetArray(var arr: IntegerArray; const len: Cardinal): PWideChar;
  2. var i:Integer;
  3. begin
  4.   Result:='Fibonacci numbers:';
  5.   if (len < 3) then exit;
  6.   arr[0]:= 0;
  7.   arr[1]:= 1;
  8.   for i := 2 to len-1 do
  9.     arr[i]:= arr[i-1] + arr[i-2];
  10. end;
   

I need the function to return with the integer array itself (without the 'Fibonacci numbers:' string).
How should it be?

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Function returns with array
« Reply #1 on: April 02, 2018, 04:06:52 pm »
I found this code below:
Code: Pascal  [Select][+][-]
  1. function SetArray(var arr: IntegerArray; const len: Cardinal): PWideChar;
  2. var i:Integer;
  3. begin
  4.   Result:='Fibonacci numbers:';
  5.   if (len < 3) then exit;
  6.   arr[0]:= 0;
  7.   arr[1]:= 1;
  8.   for i := 2 to len-1 do
  9.     arr[i]:= arr[i-1] + arr[i-2];
  10. end;
   

I need the function to return with the integer array itself (without the 'Fibonacci numbers:' string).
How should it be?

Untested code:

Code: Pascal  [Select][+][-]
  1. type
  2.   IntegerArray = array of Integer;
  3.  
  4. function SetArray(const len: Cardinal): IntegerArray;
  5. var i:Integer;
  6. begin
  7.   SetLength(Result, len);
  8.   if (len < 3) then
  9.   begin
  10.     SetLength(Result, 0);  //returns an empty array
  11.     exit;
  12.   end;
  13.   Result[0]:= 0;
  14.   Result[1]:= 1;
  15.   for i := 2 to len-1 do
  16.     Result[i]:= Result[i-1] + Result[i-2];
  17. end;
   

[edit]Changed all occurrencies of "arr" into "Result" inside the function.

Bart
« Last Edit: April 02, 2018, 04:18:28 pm by Bart »

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Function returns with array
« Reply #2 on: April 02, 2018, 04:13:14 pm »
Looks good, thank you!
New question: how will this look like if I need the return as a pointer of the integer array?

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Function returns with array
« Reply #3 on: April 02, 2018, 04:16:46 pm »
Dynamic arrays are in fact pointers...

But if you need to (again, untested):

Code: Pascal  [Select][+][-]
  1.     type
  2.       IntegerArray = array of Integer;
  3.       PIntegerArray = ^IntegerArray;
  4.      
  5.     function SetArray(const len: Cardinal): PIntegerArray;
  6.     var i:Integer;
  7.     begin
  8.       SetLength(Result^, len);
  9.       if (len < 3) then
  10.       begin
  11.         SetLength(Result^, 0);  //returns an empty array
  12.         exit;
  13.       end;
  14.       Result^[0]:= 0;
  15.       Result^[1]:= 1;
  16.       for i := 2 to len-1 do
  17.         Result^[i]:= Result^[i-1] + Result^[i-2];
  18.     end;

Bart

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Function returns with array
« Reply #4 on: April 02, 2018, 04:22:10 pm »
Great, thank you!
Can I ask that someone may explain "my" original code? I don't understand the working of "Result".
In the beginning there is Result:='Fibonacci numbers:';
But, how (why) array values are added to Result?
« Last Edit: April 02, 2018, 04:29:53 pm by justnewbie »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Function returns with array
« Reply #5 on: April 02, 2018, 04:29:17 pm »
I don't understand the working of "Result".

Result is a "built in" variable in functions to which you can assign, well, the result.

Old style Turbo Pascal:

Code: Pascal  [Select][+][-]
  1. function Add(a,b: Integer) Integer;
  2. begin
  3.   Add := a + b;
  4. end;

New/current style pascal:
Code: Pascal  [Select][+][-]
  1. function Add(a,b: Integer) Integer;
  2. begin
  3.   Result := a + b;
  4. end;

It does the same, but it's easier to read and most of the times shorter to write (consider the function name would be AddAnIntegerToAnotherIntegerAndReturnTheResultOfThisAdding  O:-)  )

Bart

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Function returns with array
« Reply #6 on: April 02, 2018, 04:32:28 pm »
It is clear, but I don't understand how/why arr[0], arr[1] ... etc are added to Result?
There is no something like this: Result:=arr[0]; etc ...

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Function returns with array
« Reply #7 on: April 02, 2018, 05:18:12 pm »
These are added to result?

These are added to

 var arr

Is like a pointer parameter in c

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Function returns with array
« Reply #8 on: April 02, 2018, 05:27:10 pm »
You maybe right. Thanks.
But, I don't understand the arr sizing. Doesn't need to make a SetLength for arr?

Also.
This is my NOT READY function. I need to get the pointer of my struct as a return.
Can you help me?
Code: Pascal  [Select][+][-]
  1. type
  2.   TSomething = packed record
  3.     myDouble: double;
  4.     myInteger: integer;    
  5.   end;
  6.  
  7.   TSArray = array of TSomething;
  8.   pTSArray = ^TSArray;
  9.  
  10. function MyFunction(var smtg: TSArray): pTSArray;
  11. var
  12.     i: integer;
  13.     counter: integer = 0;
  14. begin
  15.     for i:=0 to 10 do
  16.     begin
  17.         inc(counter);
  18.         SetLength(smtg,counter);  
  19.         smtg[i].myDouble:=i*0.1;
  20.         smtg[i].myInteger:=i;
  21.     end;
  22. end;

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Function returns with array
« Reply #9 on: April 02, 2018, 06:04:38 pm »
But, I don't understand the arr sizing. Doesn't need to make a SetLength for arr?

No, that method is something like in C, you pass the pointer and the size of the array. That means the length is already set somewhere else.

That is not needed in Pascal because you can call Length(arr) and get the size.

This is my NOT READY function. I need to get the pointer of my struct as a return.
Can you help me?

Some things:
- You don't need to set Length in each iteration, set it only one time.

Code: Pascal  [Select][+][-]
  1. SetLength(smtg,10);
  2. for i:=0 .....
  3.  

You don't need the counter variable.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Function returns with array
« Reply #10 on: April 02, 2018, 06:42:29 pm »
You need a procedure, not a function, unless you need a copy....in which case var is wrong... Here's a working example
Code: Pascal  [Select][+][-]
  1. program TestCase;
  2. type
  3.   Tsomething = record
  4.     MyDouble:double;
  5.     MyInteger:integer;  
  6.   end;
  7.   TSarray = array of Tsomething;
  8.  
  9. procedure MyProcedure(var smtg: TSArray);
  10. var
  11.   i:integer;
  12. begin
  13.     for i := 0 to pred(length(smtg)) do
  14.     begin  
  15.         smtg[i].myDouble:=i*0.1;
  16.         smtg[i].myInteger:= i;
  17.     end;
  18. end;
  19. begin
  20. end.

« Last Edit: April 02, 2018, 06:44:57 pm by Thaddy »
Specialize a type, not a var.

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Function returns with array
« Reply #11 on: April 02, 2018, 07:04:30 pm »
@lainz: The 10 is only in the example, really it is unknown in advance, so I need the counter.
@Thaddy: I need a function, because it (Lazarus DLL) has to communicate with an other program (MT4). I think procedure is not good for it (maybe I'm wrong).

So, what is the solution of my question?
« Last Edit: April 02, 2018, 07:12:25 pm by justnewbie »

justnewbie

  • Sr. Member
  • ****
  • Posts: 292
Re: Function returns with array
« Reply #12 on: April 02, 2018, 08:55:38 pm »
Anyone can help me (based on my example code from Reply #8)?
In short: I need to give an array of a struct to an external program (from a DLL to MT4).

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Function returns with array
« Reply #13 on: April 02, 2018, 11:17:03 pm »
@justnewbie: what exactly is your function supposed to do? What is the purpose of the smtg parameter and what should be included in the array returned from the function?
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Function returns with array
« Reply #14 on: April 03, 2018, 12:26:35 am »
When ever you have a (Var name:type)  in there (VAR) the parameter has been past to the
function/procedure by way of a pointer..

 So while in the Block of code you can make changes to this NAME :Type and the changes will take place
back at the source of where you originally called the function in the DLL, that would be the MAIN app.

 This is called a REFERENCE parameter and what ever changes you make are actually made at the original
source.

 Normally in DLL's, you use simple types for return types, in your case a BOOLEAN would be a good idea or
some integer to indicate a state of the function upon return..

 DLL's normally return large chuncks of data via a Pointer or a provided Pointer parameter ..

come back when you need more info.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018