I am trying following code to square a dynamic array of integers:
program mysquare_prog;
{$mode objfpc}
type
tarr = array of integer;
var
arr : tarr;
function mysquare(sarr: tarr): tarr;
var
outarr: tarr;
i: integer;
begin
outarr := tarr.create;
outarr.setlength(length(sarr));
for i in [0..length(sarr)-1] do
outarr[i] = sarr[i] * sarr[i];
result := outarr;
end;
begin
arr := tarr.create();
setlength(arr, 10);
arr.add(1);
arr.add(2);
arr.add(3);
writeln(mysquare(arr));
end.
However, I am getting following error:
$ fpc rnmaparr.pas
Free Pascal Compiler version 3.0.0+dfsg-11+deb9u1 [2017/06/10] for i386
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for i386
Compiling rnmaparr.pas
rnmaparr.pas(16,9) Fatal: Syntax error, "CREATE" expected but "SETLENGTH" found
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode
I have already used create in previous statement but it is still looking for create. I suspect the problem is very basic since I have just started using Pascal. Thanks for your help.