Recent

Author Topic: [Solved] Function Return/Result question  (Read 1272 times)

dryzone

  • New Member
  • *
  • Posts: 13
[Solved] Function Return/Result question
« on: March 14, 2019, 09:51:18 am »
Iam porting old Borland Programs of mine to freepasal, and land into some trouble of programs that compile and run on borland tp6 but not in fpc.

Below part of the interfce of a unit.

What is the way free pascal impliments the return of an array assigned to a function.
In this case we have Matrix as the result we want to equate the function return to.
would it be
FileToMatrix:=Matrix;
in fpc
or do I need to equate the function and Matrix components as a loop.
In both cases it fails in freepascal when executed,but compiles without error..



Interface

uses sysutils;
type
OPMatrix = array of array of Real;
var
Matrix :array of array of Real;
   
function FileToMatrix(FileName : String;NoOfCols:LongInt ): OPMatrix;

« Last Edit: March 15, 2019, 05:11:27 pm by dryzone »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7363
Re: Function Return/Result question
« Reply #1 on: March 14, 2019, 09:59:45 am »
This is weird , since Turbo Pascal does not have dynamic arrays.

Thaddy

  • Hero Member
  • *****
  • Posts: 8685
Re: Function Return/Result question
« Reply #2 on: March 14, 2019, 10:13:35 am »
Yes. That can't be the case.
BTW your code doesn't even compile with TP5.5....
(Which you can still get for free and runs in e.g. DosBox)

I do not know what you are porting, but both Marco and me are really 30 years + (well, more) Pascal progammers.  And a bit  educated... Somewhat... O:-)
« Last Edit: March 14, 2019, 10:20:21 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

lucamar

  • Hero Member
  • *****
  • Posts: 1991
Re: Function Return/Result question
« Reply #3 on: March 14, 2019, 11:29:18 am »
It won't compile either in TP6 or BP7. Which is, of course,  logical. Anyway, you say:
In both cases it fails in freepascal when executed,but compiles without error..

But you don't show us any real code and don't tell us how it fails: a runtime error? an exception? where?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

dryzone

  • New Member
  • *
  • Posts: 13
Re: Function Return/Result question
« Reply #4 on: March 14, 2019, 09:02:41 pm »
Well then I must have converted it already a while ago and forgot I did so when it didnt work out, I might be having a problem with fpc only then. It was probably using Lazarus. So it might not be BP anymore.
Anyway.

So how do I load the values of the array variable "Matrix" into the result of the function.

function FileToMatrix(FileName : String;NoOfCols:LongInt ): OPMatrix; 

I seem to fail being able to do this in fpc
Is it
FileToMatrix:=Matrix;
or
Result:=Matrix
or having
FileToMatrix[i,k]=Matrix[i,k] in a loop.
I tried them all and it would for some of the options just give an empty result.
or exit with
An unhandled exception occurred at $00000000004232F1:
EAccessViolation: Access violation
  $00000000004232F1
  $000000000040023A

I am not interested in chestbeating or edu-ca-shinnn selfies sic, I am only interested how fpc  passes data from an array variable
called "Matrix" to a function which is of same type  as "Matrix"
It is that simple.

« Last Edit: March 14, 2019, 09:33:07 pm by dryzone »

jamie

  • Hero Member
  • *****
  • Posts: 1925
Re: Function Return/Result question
« Reply #5 on: March 14, 2019, 09:43:58 pm »
Arrays without size are dynamic..

 I don't know what you were doing at some earlier date but I can tell you what you are doing now
isn't working!

 So you need to set the length of the array before you store anything in them...

 SetLength(MyArray, Size?, Size?)

 Arrays are 0 based, which means the first index to the array is 0 and the last index is one less than the
size.

 or you could use the HIGH to obtain the highest index to use.

work on that.

dryzone

  • New Member
  • *
  • Posts: 13
Re: Function Return/Result question
« Reply #6 on: March 15, 2019, 04:00:17 am »
Thank you, I did that in my program for array "Matrix" and did not make that mistake.
 SetLength(Matrix, NoOfCols, NoOfRows);

Since the function returns the contents of array "Matrix", does the function also somehow need to be treated with setlength ?
I dont think so, but I rather ask.

I tested the contents of the array "Matrix" and it contains all the values.
It is just that when I assign it to the function that the data is not transferred to the function.

So, all I can think of is that the function also needs to be SetLength, however doubtful, it does make sense as everything else works except the transfer from Matrix to the function.


lucamar

  • Hero Member
  • *****
  • Posts: 1991
Re: Function Return/Result question
« Reply #7 on: March 15, 2019, 04:53:50 am »
Have you tried declaring Matrix as OPMatrix? As in:
Code: Pascal  [Select]
  1. Interface
  2.  
  3. uses sysutils;
  4. type
  5. OPMatrix = array of array of Real;
  6. var
  7. Matrix :OPMatrix;
  8.    
  9. function FileToMatrix(FileName : String;NoOfCols:LongInt ): OPMatrix;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

vangli

  • New Member
  • *
  • Posts: 18
Re: Function Return/Result question
« Reply #8 on: March 15, 2019, 08:41:17 am »
I guess you have read http://wiki.freepascal.org/Dynamic_array. Especially the statement "Assigning dynamic array variables to each other does not copy any payload, but just the address. This differs from static arrays' behavior."

« Last Edit: March 15, 2019, 08:55:48 am by vangli »
Regards Bent

mangakissa

  • Hero Member
  • *****
  • Posts: 935
Re: Function Return/Result question
« Reply #9 on: March 15, 2019, 09:44:14 am »
Something like this:
Code: Pascal  [Select]
  1. type
  2.  
  3. Matrix = array of array of double;
  4.  
  5. TForm1 = class(TForm)
  6.     Memo1: TMemo;
  7.     procedure FormShow(Sender: TObject);
  8.   private
  9.   public
  10.   end;
  11.  
  12. var
  13.   Form1: TForm1;
  14.   OPMatrix : Matrix;
  15.   teller, teller2 : integer;
  16.  
  17.   function Fill : Matrix;
  18.  
  19.  
  20. implementation
  21.  
  22. {$R *.lfm}
  23.  
  24. function Fill : Matrix;
  25. var M    : matrix;
  26.     X, Y : double;
  27. begin
  28.   X := 0;
  29.   Y := 0.0;
  30.   for teller := 0 to 10 do
  31.   begin
  32.     X := X + 1;
  33.     Setlength(M,teller + 1);
  34.     Y:= X;
  35.     for teller2 := 0 to 10 do
  36.     begin
  37.       Y := Y + 0.1;
  38.       Setlength(M[teller],teller2 + 1);
  39.       M[teller,teller2] := Y;
  40.     end;
  41.   end;
  42.   Result := M
  43. end;
  44.  
  45. { TForm1 }
  46.  
  47. procedure TForm1.FormShow(Sender: TObject);
  48. begin
  49.   OPMatrix := fill;
  50.   for teller := 0 to 10 do
  51.   begin
  52.     for teller2 := 0 to 10 do
  53.       memo1.lines.add(FloatToStr(OPMatrix[teller,teller2]));
  54.   end;
  55. end;
  56.  
  57.  
  58. end.
  59.  
It compiles but when running it crashes when the second array will be created  (but not in Delphi)::)
« Last Edit: March 15, 2019, 10:02:45 am by mangakissa »
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

dryzone

  • New Member
  • *
  • Posts: 13
Re: Function Return/Result question
« Reply #10 on: March 15, 2019, 05:10:46 pm »
That was exactly my problem.
It works now.
I keep on thinking fpc is BP and that it is Lazarus that has dynamic arrays.
It is ingrained thinking from BP to Delphi that I keep applying to fpc and Lazarus which is obviously flawed.

I dont know if this thread will be any use to any readers.
If it is too trivial for the Moderator, it can be leted if it has no perceived purpose.
I had a conceptual problem rather than a programming problem.

Thanks to all that helped.
This is now solved.

I guess you have read http://wiki.freepascal.org/Dynamic_array. Especially the statement "Assigning dynamic array variables to each other does not copy any payload, but just the address. This differs from static arrays' behavior."
« Last Edit: March 15, 2019, 09:07:52 pm by dryzone »