Wish I would like to have Optional Parameters for Subs

Widget

Well-Known Member
Licensed User
Longtime User
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:

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.
 
D

Deleted member 103

Guest
Why do you misquote me? I never said that it will be added in the next version.

Please don't do it.
I'm sorry, :(
but for me is there no big difference between the "next version" and the "future version".
 
D

Deleted member 103

Guest
I also didn't say that it will be added in a future version. I wrote:

"This feature may be added in a future version."

If you quote me in the forum then please post what I wrote exactly.
fakt is that you said it 5 years ago, and still nothing has happened.
Please think about it, the people are waiting.
 

Widget

Well-Known Member
Licensed User
Longtime User
Erel,

Is it difficult to implement optional parameters for Subs? From the thread that was started 4 years ago a lot of people thought it was a good idea.
When you have time, can you re-evaluate implementing it?
TIA
 
Top