2

The following code:

program Default_Issue;

function SomeFunction(var myVar : integer; 
   defaulted : boolean = true) : integer;
begin
   writeln('Inside SomeFunction');
   SomeFunction := 0
end;

var 
   i :  integer;

begin
   SomeFunction(i,true);
end.

Gets a compiler error pointing to the = sign in the function declaration:

Default_Issue.pas(3,64) Fatal: Syntax error, ")" expected but "=" found

The program will compile if the function declaration line has the default parameter value removed:

function SomeFunction(var myVar : integer; 
     defaulted : boolean) : integer; 

In the Free Pascal manual it shows default parameters as an option for value parameters and the syntax appears to be "var-name : type = value" so I don't understand why I am getting the compiler error.

3 Answers 3

4

Features are divided over languages modes. Default parameters is a delphi feature and some of those are only enabled in the objfpc and Delphi dialects.

The default (FPC) dialect is Turbo Pascal compatible, and TP didn't have default parameters. Add -S2 or -Sd parameters. (FPC mode has overloading though, which can be used to mimic it)

Sometimes there is confusion because Lazarus defaults to objfpc mode by passing extra parameters.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That did the trick (I used the {$mode objfpc} code flag.
(Cleaned up the quick earlier comment)
3

Free Pascal has five compiler modes: (FPC, OBJFPC, TP, DELPHI, MACPAS).

The default compiler mode is FPC. Default function/procedure parameters are not supported in FPC mode. To use them you have to change the compiler mode to one that supports default function/procedure arguments. This can be done in the source code or on the command line. Default parameters are supported in the OBJFPC mode (the extended Free Pascal mode that gives you all the Free Pascal features) and also in the DELPHI mode (which aims for as much Delphi compatibility as possible).

To change the compiler mode in source code use the {$mode xxxx} compiler directive:

Program UseDefaultParameters;

{$mode OBJFPC}

The people who wrote the Lazarus IDE must think that this should be the default mode as they automatically add this source code compile mode whenever you ask it to create a new (Unit, Application, Program, Console Application, Library, InstantFPC program, FPCUnit Console Test Application, FCPUnit Test Application) file. The only time that the Lazarus IDE creates a file that doesn't have the OBJFPC compiler mode set is when it creates a new Simple Program.

To change the compiler mode on the command line use the -M compiler mode switch:

fpc -Mobjfpc my_program.pas

Comments

1

As I can see, your problem is in the function declaration, unexpected bracket after integer:

wrong:

function SomeFunction(var myVar : integer; defaulted : boolean = true) : integer )

right:

function SomeFunction(var myVar : integer; defaulted : boolean = true) : integer;

1 Comment

Oh, thanks. That may be a result of commenting and uncommenting. But I have removed it, and the error is the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.