PDA

View Full Version : Sub/Function-Optional-parameters



ReneMiner
29-10-2012, 23:00
the help-file does not tell me, but I want to know what happens with Optional arguments?

can I set some default value to missing optional arguments somehow?

ErosOlmi
30-10-2012, 07:56
Yes it is possible but only in a limited way: only numeric parameters and only if passed BYVAL

http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?function_default_parameter_val.htm

REDEBOLT
30-10-2012, 11:51
In looking at the supplied example, I see that

MyFunction(,)

has two missing arguments, so it would be

MyFunction(p1,p2).

But the printed result shows

100 100 -1 0.

Since p1 = 100, and p2 = 200, I think the result should be

100 200 -1 0.

Where have I gone wrong?

Regards
Bob

ErosOlmi
30-10-2012, 12:16
Hi Bob,

right you are.
I will open an issiue in Support.

http://www.thinbasic.com/community/project.php?issueid=366

ErosOlmi
01-11-2012, 16:18
Fixed, will be present in next 1.9.x beta update

ReneMiner
13-06-2013, 11:35
Two things:

1. I'm not sure about and want to know:



Sub A(Optional X , Y)

Sub B(Optional X, Optional Y)


is there a difference? Is B valid?

2. By searching help for Optional I found in
Navigation: thinBasic language (http://www.thinbasic.com/community/thinbasiclanguage.htm) > Script structure (http://www.thinbasic.com/community/scriptstructure.htm) >Functions/Subs

an example:



Function Factorial(InVal AsNumber) As Number
Dim Count AsLong
Dim RetVal AsLong

RetVal = 1
For Count = 2 To InVal
RetVal = RetVal * i
Next

Function = RetVal

End Function




where does i in line 7 come from ? ;)

ErosOlmi
16-06-2013, 11:28
both are valid, thinBasic has some ... flexibility :)
"Optional" is considered just a marker: from the first "optional" param, all subsequent parameters will be automatically "optional"
But remember: not defined variable types are interpreted as VARIANT type. If you debug this little example you will see it.

Uses "Console"

Sub A(Optional X , Y)
PrintL Function_Name, x, y
End Sub



Sub B(Optional X, Optional Y)
PrintL Function_Name, x, y
End Sub


a(1,2)
b(3,4)
WaitKey
My error, fixed. Variable "I" is instead "Count"


Thanks
Eros