Sign in Join
Wiki copytemporary

Default parameter

Deutsch · English · Español · Français · Polski · Suomi · Русский

A default parameter also referred to as optional argument (or default argument) is a function or procedure parameter that has a default value provided to it. If the programmer does not supply a value for this parameter, the default value will be used. If the programmer does supply a value for the default parameter, the programmer-supplied value is used.

The programmer can extend an existing function or procedure by adding some parameters that have default value instead of writing an identical method with different parameters.

Beware: Once in a parameter list an optional parameter is defined, all following parameters have to be optional, too. A mandatory parameter must not have a later position than an optional parameter.

Example deadlineDay

pascal
{$mode objfpc}

uses SysUtils, DateUtils;

function deadlineDay(day: integer; month: integer = 0): TDateTime;
var
  n: TDateTime;
  y, m, d: word;
begin
  n := now();
  DecodeDate(n, y, m, d);
  if month = 0 then
  begin
    if d > day then
    begin
      n := IncMonth(n);
      DecodeDate(n, y, m, d);
    end;
  end
  else
  begin
    if month < m then
    begin
       n := IncYear(n);
    end;
    decodeDate(n, y, m, d);
    m := month;
  end;
  d := day;
  result := EncodeDate(y, m, d);
end;

Default parameter is used in place of the missing trailing parameter in a call


From the Free Pascal wiki, page "Default parameter", as it stood on 2020-02-12 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.