Contents | Prev | Next

11.1 What are program parameters?

Description

Program parameters are identifiers which appear at the top of the program, in the program heading, after the program name and between parentheses. In addition to appearing after the program name, most program parameters must also appear in a variable declaration in the main program block. Two special program parameters input and output are the exceptions to this rule. The appearance of the two special program parameters in the program heading automatically declares them as file variables, associated with the standard-input and standard-output streams.

You can use program parameters to access the command-line arguments passed to your program. NOTE: You can also use paramcount and paramstr to access command-line arguments.

Example

For example, suppose you want to write a program to append two files together, writing the appended files to a third file, then you might write a program similar to the program below.

   (**********************************************************************
   ** This program appends two files together, writing the appended files
   ** out to a third file.
   *)
   program append(in1, in2, out, output);
   type
      char_file = file of char;
   var
      in1 : char_files;     (* first input file *)
      in2 : char_files;     (* second input file *)
      out : char_files;     (* output file *)

      (***************************************************************
      ** PURPOSE: Writes copies the contents of one file into another.
      ** ARGUMENTS:
      **    'f' - the input file
      **    'g' - the output file
      ** NOTES: It is up to the caller to open and close the files.
      *)
      procedure WriteFile(var f, g: char_file);
      var
     c : char;
      begin
     while not eof(f) do
        begin
           read(f, c);
           write(g, c)
        end
      end;

      (**********************************************
      ** PURPOSE: Writes a help screen and then halts
      *)
      procedure syntax;
      begin
         writeln('Appends two files together and writes the output to a third file');
         writeln('Syntax');
         writeln('   ivm append in1 in2 out');
         writeln('where "in1" is the first input file');
         writeln('and   "in2" is the second input file');
         writeln('and   "out" is the output file');
         halt
      end;

   begin
      if paramcount <> 3 then
         syntax;
      rewrite(out);
      reset(in1);
      WriteFile(in1, out);
      close(in1);
      reset(in2);
      WriteFile(in2, out);
      close(in2)
      close(out);
   end.

The first thing to notice about this program is the line below:

   program append(in1, in2, out, output);

Here four program parameters are being used:

  1. in1 - accesses the first command-line argument.
  2. in2 - accesses the second command-line argument.
  3. out - accesses the third command-line argument.
  4. output - does not access a command-line argument.
Since output does not access a command-line argument it can appear in any position (not just the last position), without changing which command-line arguments are accessed by the other program parameters. In other words all of the following are equivalent

   program append(output, in1, in2, out);

and

   program append(in1, output, in2, out);

and

   program append(in1, in2, output, out);

and

   program append(in1, in2, out, output);

The second thing to notice about the program are the following lines.

   var
      in1 : char_files;     (* first input file *)
      in2 : char_files;     (* second input file *)
      out : char_files;     (* output file *)

Here the program parameters (except for output) appear in variable declarations, in the main program block, as required. They are declared to be variables of type char_file which has been declared to be

   file of char

Now since they are declared to be file variables it means that the command-line arguments accessed specify the initial names of the file variables.

The final thing to notice about the program are the following lines.

      if paramcount <> 3 then
         syntax;

These lines cause the procedure syntax to be called if the number of command-line arguments is not 3. These lines are intended to prevent problems if the user does not enter 3 command-line arguments.

So far only file type program parameters have been described. You can also use string type program parameters. They can also access command-line arguments, but in a different way. The command-line argument accessed is simply assigned to the string type program parameter.

If you use program parameters other than file type or string type then the command-line argument is ignored. The compiler will issue a warning, that the command-line argument has an invalid type, but otherwise do nothing.

For example look at this rather strange program.

   program strange(f, s1, dummy, s2);
   var
      f : text;
      s1, s2 : string;
      dummy : real;
   begin
      rewrite(f);
      writeln(f, s1);
      writeln(f, s2)
   end.

If you compile it, you will get some warnings but ignore them. If you run the program with

   ivm strange out.txt first skip second

then the first program parameter f will access out.txt, and since f is a file type program argument, when rewrite is used on f, the file out.txt will be opened. The second program parameter s1 will access first, and since this is a string type program argument, then first will be assigned to s1. The third program parameter dummy is not a file type or string type program parameter so the third command-line argument skip will be ignored. The fourth program parameter s2 will access second, and since this is a string type program argument then second will be assigned to s2.

So the effect of the following three lines

      rewrite(f);
      writeln(f, s1);
      writeln(f, s2)

is that a text file out.txt is opened and two lines are written to it. The first line will be first and the second will be second.

Syntax

(NOTE: for clarity some parts of the syntax are omitted, see Irie Pascal Grammar for the full syntax):

   program = program-heading ';' program-block

   identifier = letter { letter | digit }

   identifier-list = identifier { ',' identifier }

   program-block = block

   program-heading = 'program' identifier [ '(' program-parameter-list ')' ]

   program-parameter-list = identifier-list

Contents | Prev | Next