Recent

Author Topic: Optional parameter in function call [SOLVED]  (Read 278 times)

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Optional parameter in function call [SOLVED]
« on: October 05, 2024, 12:49:23 pm »
* Mac Mini M1
* macOS 14.6.1
* Lazarus 3.99
* FPC 3.3.1

I'm planning to use a function with the signature...
Code: Pascal  [Select][+][-]
  1. function CreateMidiClient(Errors: TStrings; MidiClient: longword = 0): Boolean;
  2.  

I've got vague memories of a restriction in Delphi, whereby the optional parameter had to be at the end of the list of parameters. Can anyone tell me, am I imagining this, and if not, does the same restriction apply to FPC?
« Last Edit: October 05, 2024, 02:24:18 pm by carl_caulkett »
"It builds... ship it!"

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Optional parameter in function call
« Reply #1 on: October 05, 2024, 01:01:43 pm »
Yes, optional has to be at the end. Otherwise, the function will not be able to tell which one you have left off .

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

440bx

  • Hero Member
  • *****
  • Posts: 4731
Re: Optional parameter in function call
« Reply #2 on: October 05, 2024, 01:35:12 pm »
You can read about it at https://wiki.freepascal.org/Default_parameter

The important addition is that there can be more than one default parameter and they all must be "trailing" parameters.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 16158
  • Censorship about opinions does not belong here.
Re: Optional parameter in function call
« Reply #3 on: October 05, 2024, 01:39:51 pm »
And that if you set the right-most parameter, you have to set all other parameters with defaults  too.
If I smell bad code it usually is bad code and that includes my own code.

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Optional parameter in function call
« Reply #4 on: October 05, 2024, 02:23:52 pm »
Thanks everyone!
"It builds... ship it!"

 

TinyPortal © 2005-2018