Difference between revisions of "Default parameter"
Line 4: | Line 4: | ||
[[Procedure|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. | [[Procedure|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. | ||
+ | == Example deadline_day == | ||
<syntaxhighlight> | <syntaxhighlight> | ||
Line 36: | Line 37: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | Default parameter is used in place of the missing trailing parameter in a call | ||
+ | |||
+ | * in program call <code> deadline_day( 5 ); </code> //-> calls <code> deadline_day( 5, 0 ); </code> | ||
+ | * in program call <code> deadline_day( 5, 1 ); </code> //-> calls <code> deadline_day( 5, 1 ); </code> |
Revision as of 11:06, 13 October 2015
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
polski (pl) │
русский (ru) │
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.
Example deadline_day
uses SysUtils, DateUtils
function deadline_day( 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 n := IncYear( n );
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
- in program call
deadline_day( 5 );
//-> callsdeadline_day( 5, 0 );
- in program call
deadline_day( 5, 1 );
//-> callsdeadline_day( 5, 1 );