Reputation:
I am trying to implement the following algorithm in Pascal. Pascal is new for me, so I can't understand what the problem is. The program attempts to find the maximum between two integers like this:
program maqsimaluri;
function max(a,b:integer):integer;
begin
if a>=b then
max:=a
else
max:=b;
end;
negon
var a:=5;
var b:=4;
write(max(a,b));
end.
But I get the following errors
Free Pascal Compiler version 2.2.0 [2009/11/16] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling prog.pas
prog.pas(10,5) Error: Illegal expression
prog.pas(10,9) Error: Illegal expression
prog.pas(10,9) Fatal: Syntax error, ";" expected but "identifier A" found
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)
What might cause this error and how do I solve it?
Upvotes: 0
Views: 6662
Reputation: 26376
Of course, unit math predefines MAX (as inline) for various types
uses math;
var
a,b: Integer;
begin
a:=5;
b:=4;
write(max(a,b))
end.
Upvotes: 2
Reputation: 245
Before saying what is wrong, I would advise you to indent your code. For example your function body should be moved to the right by two spaces. This helps you to catch some errors and improves readability.
Now the error: I do not really know, why there is a 'negon' instruction in your code, but on ideone it is not there. But this is not the problem, as your variable declarations are wrong. First, they are in the wrong place, because they should be put before the keyword 'begin' of the main program. As far as I know, you cannot declare variables in the code in Pascal, but you have to do it beforehands. Second, you must specify the type, the variables have. Pascal is a strong, typesafe language and checks, if variable values fit its type. In this case it is propably 'Integer'. Third, you cannot give the variables values in its declaration. You have to do that later on in the code.
I would advise you to read some basic articles about Pascal programming. For the basics, even the wikibook Pascal Tutorial from Wikipedia will be enough.
This is a version of your code, that actually runs and provides the correct output:
program maqsimaluri;
function max(a,b:integer):integer;
begin
if a>=b then
max:=a
else
max:=b
end;
var
a,b: Integer;
begin
a:=5;
b:=4;
write(max(a,b))
end.
Upvotes: 4