Jump to content
Ian Branch

Set a default parameter for a function??

Recommended Posts

Hi Team,

D12.

I have the following function:

function GetWeekDates(const GivenDate: TDateTime; const SOWDay: string; out startDate, endDate: TDateTime): Boolean;

I want to modify it so that SOWDay has a default value of 'SU' if SOWDay isn't supplied.

As SOWDay must be By-Value or a Constant, I can't do this:

function GetWeekDates(const GivenDate: TDateTime; var SOWDay: string = 'SU'; out startDate, endDate: TDateTime): Boolean;

is what I am after possible?

 

Delete Reference:  I did it with an overloaded function.

 

Regards & TIA,

Ian

Edited by Ian Branch

Share this post


Link to post

You can specify default parameter values in a procedure or function heading. Default values are allowed only for typed const and value parameters. To provide a default value, end the parameter declaration with the = symbol followed by a constant expression that is assignment-compatible with the parameter's type.  Parameters with default values must occur at the end of the parameter list. That is, all parameters following the first declared default value must also have default values.

function GetWeekDates(const GivenDate: TDateTime; out startDate, endDate: TDateTime; const SOWDay: string = 'SU'): Boolean;

 

  • Like 3
  • Thanks 1

Share this post


Link to post
1 hour ago, FPiette said:

all parameters following the first declared default value must also have default values.

Ahhhhhhh.  That was what I was missing.  I was not aware of that.

Thank you FPiette.

 

I will just have to remember the order of parameters when I call the function.  😉

 

Regards,

Ian

Share this post


Link to post

If you don't want to change the order of parameters you can use two overloaded  functions like this:

interface

function GetWeekDates(const GivenDate: TDateTime; const SOWDay: string; out startDate, endDate: TDateTime): Boolean; overload;
function GetWeekDates(const GivenDate: TDateTime; out startDate, endDate: TDateTime): Boolean; overload;

implementation

function GetWeekDates(const GivenDate: TDateTime; const SOWDay: string; out startDate, endDate: TDateTime): Boolean;
begin
  //...
end;

function GetWeekDates(const GivenDate: TDateTime; out startDate, endDate: TDateTime): Boolean;
begin
  Result := GetWeekDates(GivenDate, 'SU', startDate, endDate);
end;

Alternatively, drop the overload and use different names like GetWeekDates and GetWeekDatesEx.

  • Like 1

Share this post


Link to post

Tks uligerhart.  That's what I did initially to get it going.

I will adapt to the reordered parameters.

 

Ian

Share this post


Link to post

Not to be negatively critical, but can I suggest that there's a discussion to be had as to whether out parameters should be an out-dated usage style.

 

 

@Ian Branch Can I take it that what you want returned from the function is EITHER a failure condition/error result OR a tuple of (startdate, enddate) ?

 

If so, this is really a function with a single return type ...

 

function GetWeekDates(const GivenDate: TDateTime; var SOWDay: string = 'SU') -> FAIL | (startdate, enddate) 

This could be known as a Nullable type, or Option type ... in Rust it is the Result type.


When you return from this function, you either have (effectively) no result, or an inhabited tuple.

 

There are quite a few published examples in Delphi of Nullable types .. .from Allen Bauer through to Spring4D.  I'm not sure all of them capture the idea in the most functional way.

 

Share this post


Link to post

Hi pmcgee,

Interesting, never come across them nor can I say I have seen them.

What does FAIL represent?

 

Ian

  • Like 1

Share this post


Link to post

Hi Ian.

A nullable or option type basically adds a single possible "uninhabited" state to a return type of any kind.

You can think of a function that can throw an exception in almost exactly the same way ... it has two possible states ... the normal return or the exception.

 

I was operating on the assumption that the boolean you returned from your original function was indicating whether you successfully created a date range.

Edited by pmcgee

Share this post


Link to post

Hi pmvgee,

Your assumption was correct.

This option has fascinated me a little but I can't help wondering if it is technology for the sake of technology...

I can't see 'out' parameters dissappearing in any forseeable future so what advantage do tuples bring to the table/keyboard?

As far as I can see, In order to implement them you need another code set to support them.  I found 'generics.tuples.pas' from Malcolm Groves, on the face of it, it seems a whole lot of code for little gain.

 

Ian

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×