2
votes

I am new in pascal I have a program which gives result .... I need to pass command line input in in given variable ip1 and ip2. Its can be achieved by ParamStr[1] but it doesn't work.

  program main;
      var 
        output : integer;
      var 
        ip1 : integer;
      var 
        ip2 : integer;
    
    function add(input1,input2:integer) : integer;
       var
       result: integer;
    begin
       if (input1 > input2) then
          result := input1
       else
          result := input2;
       add := result;
    end;
    
    begin
      ip1 := 2533;**{ command line input}**
      ip2 := 555;**{ command line input}**
    
      output := add(ip1,ip2);
      writeln( ' output : ', output );
    end.K
2
What exactly do you mean by its not works? Are you having trouble converting the ParamStr values to integer, or what? - 500 - Internal Server Error
Your function add should be called max because it does not add the arguments but returns the maximum. - gammatester
Hello all, Its Not matter what my function name my problem is how can i pass ip1 and ip2 value using command line. is there any function like args or argv in C or something else.... .pls help... - Nirbhav Gupta

2 Answers

3
votes

As the other answer says, you use ParamCount and ParamStr to access command line parameters.

ParamCount returns the number of parameters passed on the command line, so you should check it first to see if you've received enough information.

ParamStr allows you to access each of the parameters passed. ParamStr(0) always gives you the full name of the executing program (including the path). Additional parameters are retrieved using the ordinal order in which they were passed, with ParamStr(1) being the first, and ParamStr(ParamCount) being the last. Each value received using ParamStr is a string value, and therefore has to be converted into the appropriate type before you can use it.

Here's a working example (pretty trivial, and all error checking omitted - you should, for instance, protect the code using StrToInt to handle errors if something is provided that won't convert to an integer).

program TestParams;

uses
  SysUtils;

var
  Input1, Input2, Output: Integer;

begin
  if ParamCount > 1 then
  begin
    Input1 := StrToInt(ParamStr(1));
    Input2 := StrToInt(ParamStr(2));
    Output := Input1 + Input2;
    WriteLn(Format('%d + %d = %d', [Input1, Input2, Output]));
  end
  else
  begin
    WriteLn('Syntax: ', ParamStr(0));  { Just to demonstrate ParamStr(0) }
    WriteLn('There are two parameters required. You provided ', ParamCount);
  end;
  WriteLn('Press ENTER to exit...');
  ReadLn;
end.

Calling it without parameters (or only one) displays the following:

C:\Temp>TestParams
Syntax: C:\Temp\TestParams.exe
There are two parameters required. You provided 0
Press ENTER to exit...

C:\Temp>TestParams 2
Syntax: C:\Temp>TestParams.exe 2
There are two parameters required. You provided 1
Press ENTER to exit...

Calling it with two parameters displays

C:\Temp\TestParams 2 2
2 + 2 = 4
Press ENTER to exit...
1
votes

You need to understand the difference between a string and an integer.

To convert betwwen an integer such as 123 and the string of characters 1 2 3 you need to use a function. strtoint is one such function, converting a string to an integer. inttostr is another, converting from an integer to a string.

The command-line data is supplied via paramstr(n) as a string.

intvar := strtoint(paramstr(n));

assigns the value of the string to the integer variable intvar.

Whereas writeln has facilities to convert an integer argument to a formatted string, the way you have used it is attempting to output a string, so you need to convert the integer output to a string.

writeln(' output : ', inttostr(output) );

should do that very nicely.


var
  x : string;
  pcnt : integer;

begin
  writeln('Parameter count=',inttostr(paramcount));
  for pcnt := 1 to paramcount do
    writeln('Parameter ',pcnt, ' of ',paramcount,' = ',paramstr(pcnt));
  readln(x);
end.

should display the parameter list.

Indeed it is the case that the writeln procedure will recognise the variable type and take steps to format the value appropriately as a string, as has arrogantly been pointed out.

The issue to me is the difference between a string and an integer. paramstr returns a string which must be converted to an integer. After more than forty years' experience in Pascal, it's my opinion that it's better for a beginner to go through the exercise of converting each way and then use the conversion facility inbuilt in writeln.

First walk, then run. You first need to understand the steps in the procedure. Then you can start using the shortcuts - once you've mastered the basics.