title: How to pass dynamic arrays in FreePascal correctly
body: <p>In this FreePascal code I write, I found that in a dynamic array of length 'n', it always contained a random value in element 'n'. </p>
  
  <p>I understand why that is, however, I am wondering if perhaps there is a flaw in the way i've written my code. I've pasted it below. I'd welcome any corrections.</p>
  
  <pre><code>program LinearSearch;
  
  {$mode objfpc}{$H+}
  
  uses
    {$IFDEF UNIX}{$IFDEF UseCThreads}
    cthreads,
    {$ENDIF}{$ENDIF}
    Classes, SysUtils
    { you can add units after this };
  
  {$R *.res}
  
  type
    ArrayOfByte = array of Byte;
  
  Function findMaximum(studentMarks : ArrayOfByte; numberOfStudents : integer)
  : integer;
  var
    maximumMark : Integer;
    studentWithMaximumMark : Integer;
    index : integer;
  begin
    setLength(studentMarks, numberOfStudents);
    maximumMark := studentMarks[0];
  
    for index:=0 to numberOfStudents do
    begin
      write(IntToStr(studentMarks[index]));
      if index = numberOfStudents then writeln('.')
      else write(',');
    end;
  
    for index:= 0 to (numberOfStudents - 1) do
    begin
        if studentMarks[index] &gt; maximumMark then
        begin
             maximumMark := studentMarks[index];
             studentWithMaximumMark := index;
          end;
    end;
    write('Maximum score of ',IntToStr(maximumMark),' was achieved by ');
    Result := studentWithMaximumMark+1;
  end;
  
  var
    studentMarks : ArrayOfByte;
    numberOfStudents : Integer;
    studentWithMaximumMarks : Integer;
    counter : integer;
  begin
    write('Enter the number of students: ' );
    readln(numberOfStudents);
    setLength(studentMarks,numberOfStudents);
  
    writeln('Input the grades for the following students:');
    for counter := 0 to (numberOfStudents - 1) do
    begin
      write('Student ',counter+1,': ');
      readln(studentMarks[counter]);
    end;
    writeln('Data has been input. Finding student with maximum marks.');
  
    studentWithMaximumMarks := findMaximum(studentMarks,numberOfStudents);
    write('student no. ',IntToStr(studentWithMaximumMarks));
    writeln;
    writeln('Press ANY key to continue');
    readln;
  end.
  </code></pre>