PDA

View Full Version : Value parameters won't compile?



deathshadow
22-08-2011, 11:06 AM
I thought I had them working in FPC before... tried to do it in one of my programs and it's a no-go; so I went right to the horses mouth.

http://www.freepascal.org/docs-html/ref/refsu55.html#x132-14200011.4.1

Their example code



program testp;

Const
MyConst = 20;

Procedure MyRealFunc(I : Integer = MyConst);

begin
Writeln('Function received : ',I);
end;

begin
MyRealFunc;
end.


Will NOT compile here in 2.4.4 -- Win 7 x64 in either the x64 or x86 versions of the compiler.

test.pas(6,34) Fatal: Syntax error, ")" expected but "=" found

I really don't want to have to write two versions of the same function on this.

deathshadow
22-08-2011, 11:09 AM
My bad, it only works when {$mode Delphi} is set. WISH they said that somewhere on said page :( No... they had to put it on the PREVIOUS page.

User137
22-08-2011, 04:49 PM
Weird, it compiles for me perfectly fine.

Lazarus 0.9.31 , FPC 2.4.3, Win7 32bit

Maybe you're missing some compiler option if directly using fpc. Lazarus is inserting alot of them when compiling.

edit: Oops, actually it's giving RunError(103) when trying to run it. Stopping on the Writeln(

deathshadow
23-08-2011, 03:36 AM
Weird, it compiles for me perfectly fine.
Lazarus throws it into delphi mode I believe, so it should compile. I can't wrap my head around RAD or visual programming, so I'm working from the command line... 30 years of programming including hand assembling my own machine language -- and I STILL can't grasp visual programming, the VCL, etc, etc... I could JUST barely understand OWL... VCL lost me...


edit: Oops, actually it's giving RunError(103) when trying to run it. Stopping on the Writeln(
If you cut/paste from the FPC website, it will bomb on the single quotes as for some screwed up reason, their code block has STYLED quotes instead of straights.

I'm coming to the conclusion the HTML version of the documentation is ***** USELESS!

User137
23-08-2011, 01:55 PM
No it's in objfpc mode

program Project1;

{$mode objfpc}{$H+}

Const
MyConst = 20;

Procedure MyRealFunc(I : Integer = MyConst);
begin
Writeln('Function received : ',I);
end;

begin
MyRealFunc;
end.

But like said i don't know what i'm doing wrong, because the app doesn't run even if i do this:

program Project1;

{$mode objfpc}{$H+}

Procedure MyRealFunc;
var i: integer;
begin
i:=1;
Writeln('Function received : ',i);
end;

begin
MyRealFunc;
end.

Compiler messages are real though, wrong changes in code do result in errors.

edit: AH! Now they run. I disabled -WG from project options so that it doesn't try to make it Win32 GUI application. From command prompt i get:

Function received : 20