Recent

Author Topic: Default parametrs doesnt work  (Read 989 times)

Parthen

  • Newbie
  • Posts: 3
Default parametrs doesnt work
« on: September 27, 2022, 10:30:06 pm »
I have some code with defaut parametrs:

Code: Pascal  [Select][+][-]
  1. procedure ScreenWrite(Character: String; CharacterColor: Word = DefaultColor; BackgroundColor: Word = DefaultBgColor);
  2. begin
  3.     TextColor(CharacterColor);
  4.     TextBackground(BackgroundColor);
  5.     Write(Character);
  6.     Restore;
  7. end;

No matter which valut I put on -- FPC give me this error:
Code: Text  [Select][+][-]
  1. Free Pascal Compiler version 3.2.2 [2022/08/31] for x86_64
  2. Copyright (c) 1993-2021 by Florian Klaempfl and others
  3. Target OS: Linux for x86-64
  4. Compiling game.pas
  5. [b]game.pas(71,63) Fatal: Syntax error, ")" expected but "=" found[/b]
  6. Fatal: Compilation aborted
  7. Error: /usr/bin/ppcx64 returned an error exitcode

Also, this code https://www.freepascal.org/docs-html/current/ref/refsu64.html#x180-20400014.4.1 also doesnt work and give me this error:

Code: Text  [Select][+][-]
  1. Free Pascal Compiler version 3.2.2 [2022/08/31] for x86_64
  2. Copyright (c) 1993-2021 by Florian Klaempfl and others
  3. Target OS: Linux for x86-64
  4. Compiling test1.pas
  5. [b]test1.pas(6,34) Fatal: Syntax error, ")" expected but "=" found[/b]
  6. Fatal: Compilation aborted
  7. Error: /usr/bin/ppcx64 returned an error exitcode
  8.  


Is something wrong in my code or its FPC problem?

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Default parametrs doesnt work
« Reply #1 on: September 27, 2022, 10:55:26 pm »
Do you have {$MODE ObjPas} at the top of your source?
The only true wisdom is knowing you know nothing

mas steindorff

  • Hero Member
  • *****
  • Posts: 532
Re: Default parametrs doesnt work
« Reply #2 on: September 27, 2022, 10:58:44 pm »
I believe what you have defined here are "optional" parameters, not just default.
since the compiler cant know if you are passing the 2nd or 3rd parameter when you only define 2 parameters for a 3 parameter procedure, you get an error. 
An optional parameter must only be the last parameter unless something has changed recently.
MAS
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Default parametrs doesnt work
« Reply #3 on: September 27, 2022, 11:17:16 pm »
I don't think so, as long as the remaining parms are defaults it should compile.

I have done this myself; it should work.

its only when you attempt to mix a non-default type in the middle etc.

But I think it depends on what mode the compiler is in because this wasn't supported in the old days.

The only true wisdom is knowing you know nothing

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Default parametrs doesnt work
« Reply #4 on: September 28, 2022, 12:25:33 am »
Code: Pascal  [Select][+][-]
  1. procedure ScreenWrite(Character: String; CharacterColor: Word = DefaultColor; BackgroundColor: Word = DefaultBgColor);
Code: Pascal  [Select][+][-]
  1. procedure ScreenWrite(const Character: String; const CharacterColor: Word = DefaultColor; const BackgroundColor: Word = DefaultBgColor);
This should fix your problem.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Default parametrs doesnt work
« Reply #5 on: September 28, 2022, 12:35:12 am »
Mas Steindorf, you are wrong; when two parameters are passed, the compiler will treat the second parameter as the second parameter and the third parameter will get the default parameter.
As Jamie says, this should compile, but only in objfpc or delphi modes.

Quote from the documentation:
Quote
The compiler must be in OBJFPC or DELPHI mode to accept default values.

Code: Pascal  [Select][+][-]
  1. procedure ScreenWrite(Character: String; CharacterColor: Word = DefaultColor; BackgroundColor: Word = DefaultBgColor);
Code: Pascal  [Select][+][-]
  1. procedure ScreenWrite(const Character: String; const CharacterColor: Word = DefaultColor; const BackgroundColor: Word = DefaultBgColor);
This should fix your problem.

What? Const should not make a difference with this.

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Default parametrs doesnt work
« Reply #6 on: September 28, 2022, 01:31:26 am »
Meanwhile
Code: Pascal  [Select][+][-]
  1. {$modeSwitch defaultParameters+}
is sufficient if you prefer a different mode other than {$mode objFPC}/{$mode Delphi}.
Yours Sincerely
Kai Burghardt

Parthen

  • Newbie
  • Posts: 3
Re: Default parametrs doesnt work
« Reply #7 on: September 28, 2022, 07:44:38 am »
Do you have {$MODE ObjPas} at the top of your source?

No, I dont know anything about it. Google also doesnt have info about it, may you give me some documentation for this?

Parthen

  • Newbie
  • Posts: 3
Re: Default parametrs doesnt work
« Reply #8 on: September 28, 2022, 07:47:25 am »
Meanwhile
Code: Pascal  [Select][+][-]
  1. {$modeSwitch defaultParameters+}
is sufficient if you prefer a different mode other than {$mode objFPC}/{$mode Delphi}.

Okay, this helps me. Thanks!

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Default parametrs doesnt work
« Reply #9 on: September 28, 2022, 09:12:38 am »
Do you have {$MODE ObjPas} at the top of your source?

No, I dont know anything about it. Google also doesnt have info about it, may you give me some documentation for this?

The $Mode directive is an important part of FPC ($ModeSwitch is as well). You can read more about the modes here.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Default parametrs doesnt work
« Reply #10 on: September 28, 2022, 12:02:38 pm »
This should fix your problem.
What? Const should not make a difference with this.
I am sorry, i had more in my mind, to what does the params write when there is no input.
Since I am new to Lazarus/FreePascal, please forgive my inexperience.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

mas steindorff

  • Hero Member
  • *****
  • Posts: 532
Re: Default parametrs doesnt work
« Reply #11 on: September 28, 2022, 07:16:45 pm »
the Wiki is a good place look for these "how to" and "what is", things you have a name for.
see if ...
https://wiki.freepascal.org/Default_parameter/pl
explains this to your satisfaction ans se can go from there.

[edit..] was checking my post and saw the text was not  English so I clicked to change.  the English and some of the others have completely different code snippets!  the code in each is good.
Therefore, if available, look into the different language tabs for more info.
 
« Last Edit: September 28, 2022, 07:48:01 pm by mas steindorff »
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

 

TinyPortal © 2005-2018