November 20, 2026•6 min read

Pascal Capstone: Why Every Gradebook Score Came Back Zero

A gradebook reads three students from a file, prints three rows, and every score is 0. The procedure filled a copy of the array, because the parameter was missing var. Add the word and the report matches the file; the compiler had already warned about the call that filled nothing.

Read in Indonesian

nilai.txt holds three students and their scores. The report prints three rows and every score is 0, then an average of 0.00. The program exits with status 0, so nothing flags a problem.

Key takeaways

  • A procedure parameter without var is a copy, so filling the array inside the procedure leaves the caller's array empty and the report prints zeros.
  • The program still exits with status 0, and fpc warns Variable "data" does not seem to be initialized at the call that was supposed to fill it.
  • var on the parameter hands the procedure the caller's array, so the records it fills survive the call.
  • reset, eof and readln read the file, and :0:2 is what turns the average into 78.33 instead of 7.8333333333333329E+001.

The report that prints zeros

Save the data as nilai.txt: one student per line, name and score separated by a semicolon.

TEXT
andi;78
budi;92
citra;65

The starter is nilai.pas. It defines a record for one student and an array type for the class. muat fills the array from the file and lapor prints it.

PASCAL
program nilai;

type
  siswa = record
    nama: string[20];
    skor: integer;
  end;
  daftar = array[1..20] of siswa;

var
  data: daftar;
  jumlah: integer;

procedure muat(data: daftar; var jumlah: integer);
var
  f: text;
  baris: string;
  pemisah, kode: integer;
begin
  jumlah := 0;
  assign(f, 'nilai.txt');
  reset(f);
  while not eof(f) do
  begin
    readln(f, baris);
    pemisah := pos(';', baris);
    jumlah := jumlah + 1;
    data[jumlah].nama := copy(baris, 1, pemisah - 1);
    val(copy(baris, pemisah + 1, length(baris)), data[jumlah].skor, kode);
  end;
  close(f);
end;

procedure lapor(data: daftar; jumlah: integer);
var
  i, total: integer;
begin
  total := 0;
  for i := 1 to jumlah do
  begin
    writeln(data[i].nama, ' ', data[i].skor);
    total := total + data[i].skor;
  end;
  writeln('rata-rata ', (total / jumlah):0:2);
end;

begin
  muat(data, jumlah);
  lapor(data, jumlah);
end.

The command below writes the data file, then compiles. -B rebuilds even when an old binary is lying around, and -vw keeps the compile quiet except for warnings.

Output
$ printf 'andi;78\nbudi;92\ncitra;65\n' > nilai.txt && fpc -B -vw nilai.pas
nilai.pas(48,12) Warning: Variable "data" does not seem to be initialized

nilai.pas(48,12) names line 48, the call muat(data, jumlah), which is supposed to fill the array. The compiler can see that the array passed there never gets a value back.

The checker

check_nilai.pas reads the program's output from the pipe, one line at a time, and compares line i with harap[i]. Those are the four lines the file should produce: three students and the average. A matching line prints PASS. A different line prints FAIL with the expected text and the text it got. Output with more than four lines, or fewer, fails too. The last line reports how many of the four passed, and ExitCode is set to 1 when any failed.

PASCAL
program check_nilai;

var
  harap: array[1..4] of string = ('andi 78', 'budi 92', 'citra 65', 'rata-rata 78.33');
  baris: string;
  i, gagal: integer;

begin
  i := 0;
  gagal := 0;
  while not eof(input) do
  begin
    readln(baris);
    i := i + 1;
    if i > 4 then
    begin
      writeln('FAIL  baris ', i, ': keluaran lebih panjang dari yang diharapkan');
      gagal := gagal + 1;
    end
    else if baris = harap[i] then
      writeln('PASS  baris ', i, ': ', baris)
    else
    begin
      writeln('FAIL  baris ', i, ': harap "', harap[i], '", dapat "', baris, '"');
      gagal := gagal + 1;
    end;
  end;
  if i < 4 then
  begin
    writeln('FAIL  cuma ', i, ' baris, seharusnya 4');
    gagal := gagal + 1;
  end;
  writeln(4 - gagal, ' of 4 checks passed');
  if gagal > 0 then
    ExitCode := 1;
end.

The starter fails all four lines.

Output
$ fpc -vw check_nilai.pas && ./nilai | ./check_nilai
FAIL  baris 1: harap "andi 78", dapat " 0"
FAIL  baris 2: harap "budi 92", dapat " 0"
FAIL  baris 3: harap "citra 65", dapat " 0"
FAIL  baris 4: harap "rata-rata 78.33", dapat "rata-rata 0.00"
0 of 4 checks passed

A copy went in, a copy came out

procedure muat(data: daftar; var jumlah: integer) has var on jumlah and no var on data. A parameter without var is passed by value, which in Pascal means the procedure gets its own copy of the array. It fills that copy with the three students, closes the file, and returns. The copy is discarded when muat returns.

jumlah did come back, because it carries var. That is why lapor prints three rows: the count is real and the rows behind it are empty. Each row prints an empty name and a zero score, which shows up as 0 with a leading space. With every score at 0, the total is 0 and the average prints as 0.00. Meanwhile the data in the main program was never written to, which is exactly what the warning said.

One word on the parameter

Put var in front of data in muat. The procedure now receives the caller's array instead of a copy of it.

PASCAL
program nilai;

type
  siswa = record
    nama: string[20];
    skor: integer;
  end;
  daftar = array[1..20] of siswa;

var
  data: daftar;
  jumlah: integer;

procedure muat(var data: daftar; var jumlah: integer);
var
  f: text;
  baris: string;
  pemisah, kode: integer;
begin
  jumlah := 0;
  assign(f, 'nilai.txt');
  reset(f);
  while not eof(f) do
  begin
    readln(f, baris);
    pemisah := pos(';', baris);
    jumlah := jumlah + 1;
    data[jumlah].nama := copy(baris, 1, pemisah - 1);
    val(copy(baris, pemisah + 1, length(baris)), data[jumlah].skor, kode);
  end;
  close(f);
end;

procedure lapor(data: daftar; jumlah: integer);
var
  i, total: integer;
begin
  total := 0;
  for i := 1 to jumlah do
  begin
    writeln(data[i].nama, ' ', data[i].skor);
    total := total + data[i].skor;
  end;
  writeln('rata-rata ', (total / jumlah):0:2);
end;

begin
  muat(data, jumlah);
  lapor(data, jumlah);
end.

The warning is gone and the checker passes. Every line matches, so the checker never sets ExitCode to 1 and prints 4 of 4 checks passed.

Output
$ fpc -B -vw nilai.pas && ./nilai | ./check_nilai
PASS  baris 1: andi 78
PASS  baris 2: budi 92
PASS  baris 3: citra 65
PASS  baris 4: rata-rata 78.33
4 of 4 checks passed

lapor still takes data without var, and that is correct. It only reads the array, so working on a copy changes nothing it prints.

When the average prints in scientific notation

Delete the :0:2 from the writeln in lapor and run the checker again. The first three lines still pass and the fourth fails with rata-rata 7.8333333333333329E+001. A real printed without a format string goes out in scientific notation, and it keeps a second space after the label. The checker catches this because it compares whole lines, and rata-rata 78.33 is not that line.

:0:2 means no minimum width and two decimals. The integer scores need no field at all, because an integer already prints without decimals.

When the file is missing

Move nilai.txt aside and run the program. reset on a file that is not there stops the program with Runtime error 2 and exit status 2, before any row is printed. The lines under that error are addresses inside your own binary.

The capstone does not handle a missing file, because the file is the input the program was written for. When fpc warns that a variable does not seem to be initialized, check the line it names first: a procedure that needs to fill a parameter requires var.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all
Struktur DataWhat a Linked List Actually Is

A linked list is a linear chain of nodes, each holding data and a pointer to the next node. Here is what that chain looks like, what it costs next to an array, and the same minimal implementation in Java, Python, and JavaScript.

PythonPython File I/O: Why the Second Read Came Back Empty

A notes script saves a line, prints it back, then counts zero. The file on disk is correct: read() left the pointer at the end, so the second call returned an empty string. Read once into a variable, or seek(0), and the count matches the file.

For developers who learn by struggling

© 2026 Inva.dev.