[fpc-pascal] same routine with different parameters?

Sven Barth pascaldragon at googlemail.com
Wed Oct 30 10:20:41 CET 2013


Am 29.10.2013 19:35, schrieb Mark Morgan Lloyd:
> waldo kitty wrote:
>> question: in simple language, how can i have a routine with differing 
>> parameters like some routines in FPC and Lazarus do? i've never 
>> attempted this before and it is completely alien to my experience but 
>> i do use the functionality quite a lot with existing routines...
>>
>> description: i have a class object with an internal routine that i 
>> want to be able to pass no or different parameters to.
>>
>> example:  procedure MyObject.MyRoutine;
>>           procedure MyObject.MyRoutine(VarA : SomeType);
>>           procedure MyObject.MyRoutine(VarA : string; VarB: integer);
>>
>> at one point, the first instance would be used... at another point 
>> one of the other instances would be used with the only difference 
>> being the parameters that are passed or not... i know that i would 
>> have individual instances with the necessary header and code for 
>> each... i just don't know if there is anything else special that 
>> needs to be done or if it can be done with an internal class object 
>> routine..
>
> Just give it a whirl as you've shown, I think you've got it right. 
> Another useful variant is giving *the* *last* parameter an optional 
> value, which will allow you to omit the parameter:
>
> procedure MyObject.MyRoutine(const VarA : string; VarB: integer= -1);
>
Default paremeters can be used for more than the last parameter. There 
is however the following caveat: if you have e.g. a function with three 
parameters and the last two have default values, but you now have a call 
where you're fine with the default value of the second (overall) 
parameter, but not with that of the third you'll nevertheless have to 
specify the second parameter.

Regards,
Sven



More information about the fpc-pascal mailing list