In Delphi procedures you can have default values for parameters. That means these parameters are optional. If the sub is called without these parameters, the default values will be used instead.
Why would you need this?
It allows for the creation of more flexible & intelligent Subs. Default parameter values will allow the sub to work on default values/settings that the user can override if necessary. So if you don't pass a view, or color or font style argument for example, it will use the default value that you have set up in the Sub. Default parameters puts more intelligence into the sub.
The way it would look in B4A is:
'If a parameter has a default value, the argument is optional in the calling rtn.
'It can be called using:
Optional parameters can only appear after required (non-optional) parameters.
So the following is legal:
I would love to see this implemented in B4X.
I use it all the time in Delphi because I can call these Subs with a minimum number of arguments most of the time, but override the optional parameters when needed.
Why would you need this?
It allows for the creation of more flexible & intelligent Subs. Default parameter values will allow the sub to work on default values/settings that the user can override if necessary. So if you don't pass a view, or color or font style argument for example, it will use the default value that you have set up in the Sub. Default parameters puts more intelligence into the sub.
The way it would look in B4A is:
B4X:
'All 3 parameters have default values and are optional
sub SumAll(Value1=0 as int, Value2=0 as int, Value3=0 as int) as Int
return Value1 + Value2 + Value3
end sub
'If a parameter has a default value, the argument is optional in the calling rtn.
'It can be called using:
B4X:
x = SumAll()
x = SumAll(123)
x = SumAll(123,456)
x = SumAll(123,456,789)
Optional parameters can only appear after required (non-optional) parameters.
So the following is legal:
B4X:
'The first parameter is NOT optional so you must call SumAll with at least one argument:
sub SumAll(Value1 as int, Value2=0 as int, Value3=0 as int) as Int
return Value1 + Value2 + Value3
end sub
'In the calling rtn:
x = SumAll() 'This gives syntax error because Value1 parameter is not optional because it does not have a default value.
x = SumAll(123) 'This works
x = SumAll(123,456) 'This works
x = SumAll(123,456,789) 'This works
B4X:
'You can of course have default values for virtually any type of parameter.
'The minumum # of parameters in this is example are the first two. The rest are optional.
sub DoList(aIndex as Int, aList1 as List, aList2=NULL as List, aView=NULL as View, aOptimized=True as Boolean)
....
end sub
I would love to see this implemented in B4X.
I use it all the time in Delphi because I can call these Subs with a minimum number of arguments most of the time, but override the optional parameters when needed.