Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Dr.Bob's Delphi Notes Dr.Bob's Delphi Clinics
 Dr.Bob on Delphi Default Parameters
See Also: Delphi Papers and Columns

Borland Delphi 4 features a number of Object Pascal language enhancements, as usual. In this article, I'll address a very special language enhancement that was originally only found in C++ development environments, namely Default Parameters.

Default Parameters
Default parameters is a feature that can also be found in C++ development environments (such as Borland C++Builder), and actually, it was already added to the Pascal compiler of Borland C++Builder 3 (see Borland C++Builder. 3 Developer's Guide page 7-17). The feature means that we can provide default argument values for parameters of procedures and functions, and can omit an actual argument in case we want to use this particular default value. Default parameters can only appear at the end of the function or procedure parameter list, and if we omit passing arguments, they start to count (and get replaced) from right-to-left.
As an example, let's consider the existing MessageDlg function:

  function MessageDlg(const Msg: string; DlgType: TMsgDlgType;
    Buttons: TMsgDlgButtons; HelpCtx: Longint): Integer;
Usually (like 9 out of 10 times), I use the MessageDlg in a fixed way: as an "information" dialog, with a certain Msg string, only an OK button, and no HelpCtx (passing 0). However, I always have to pass all four arguments anyway, which often makes me feel I'm coding far too much (or should be doing things easier anyway).
Using three Default Parameters, I can now implement my own version of the MessageDlg function as a wrapper around the original MessageDlg. And in doing so, I can make sure that from now on (9 out of 10 times) I only have to pass the "Msg" string argument to my version of MessageDlg:
  program Delphi4;
  uses
    Dialogs;

    function MessageDlg(const Msg: String;
                              DlgType: TMsgDlgType = mtInformation;
                              Buttons: TMsgDlgButtons = [mbOK];
                              HelpCtx: LongInt = 0): Integer;
    begin
      Result := Dialogs.MessageDlg(Msg, DlgType, Buttons, HelpCtx)
    end;

  begin
    MessageDlg('Default Parameters in Delphi 4')
  end.
Note that I can still include values for the three default parameters: they will be filled from left-to-right (and hence omitted from right-to-left, as I stated earlier). So, for a YesNo confirmation dialog, without a specified HelpCtx value, I could simply call the following:
  MessageDlg('More New Language Features in Delphi 4?', mtConfirmation, [mbYesNo])
Default parameters are a great way for programmers to write less (and probably much clearer) code. Default arguments in declarations should be used to denote - indeed - default values and default behaviour, where we don't even need to specify the actual values.
The feature is probably supported by the compiler front-end, as the actual values of the default parameters are substitued and passed to the routine anyway (so it's actually more a pre-processor thing), and as far as I can see it will have no impact on overall performance of the final executable.

Overloading and default parameters
When using both method overloading and default parameters, don't introduce ambiguous method signatures. These are considered errors by the compiler. For example, your application can't include two declarations such as the following:

  procedure Answer(I: Integer; J: Integer = 0); overload;

  procedure Answer(Size: Integer); overload;

This is illegal because it is impossible to tell which procedure is meant by a call to:

  Answer(42);


This webpage © 2000-2015 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.