PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Function with optional UDT parameter  (Read 575 times)

Pierre Bellisle

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 57
Function with optional UDT parameter
« on: December 22, 2016, 04:40:34 PM »

Hey,
I want a function with an optional UDT parameter.
I could use overload, witch is a superb concept, but in this particular case I would prefer the optional way.

From you guys, is this the better way to do it?
I mean by using "Common BasketEmpty As Fruit" to set the filling UDT.

Thank,
Pierre

Code: [Select]
#Include Once "windows.bi"

TYPE Fruit
  Banana     As WORD
  Clementine As DWORD
  Coconut    As DOUBLE
  Lime       As WORD
End type

Common BasketEmpty As Fruit '=> {2, 5, 1.5, 2} 'COMMON variables cannot be initialized

Function DoGrocery(ByVal sStore As String, ByVal Basket As Fruit = BasketEmpty) As DWORD

 Print Basket.Banana

End Function

Dim Basket As Fruit

Basket.Banana = 2

DoGrocery("Store", Basket)

DoGrocery("Store")

Print "Press a key or click to end"
Dim buttons As Long
Do
  GetMouse(0, 0, 0, Buttons) : IF buttons Or Len(InKey) Then Exit Do : Sleep 200   
Loop
« Last Edit: December 22, 2016, 04:42:18 PM by Pierre Bellisle »
Logged

Josť Roca

  • FireFly3 Registered User
  • Master FireFly Member
  • *
  • Posts: 2785
    • Josť Roca Software
Re: Function with optional UDT parameter
« Reply #1 on: December 22, 2016, 04:58:09 PM »

I think that the best way is

Code: [Select]
Function DoGrocery(ByVal sStore As String, ByVal Basket As Fruit PTR = NULL) As DWORD
   IF Basket then Print Basket->Banana

> Common BasketEmpty As Fruit '=> {2, 5, 1.5, 2} 'COMMON variables cannot be initialized

But if the purpose is to have default values, then you can use:

Code: [Select]
Function DoGrocery(ByVal sStore As String, ByVal Basket As Fruit = TYPE(2, 5, 1.5, 2)) As DWORD
 Print Basket.Banana

Josť Roca

  • FireFly3 Registered User
  • Master FireFly Member
  • *
  • Posts: 2785
    • Josť Roca Software
Re: Function with optional UDT parameter
« Reply #2 on: December 22, 2016, 05:04:19 PM »

You can also use a define, e.g.

Code: [Select]
#define BasketEmpty type(2, 5, 1.5, 2)

Function DoGrocery(ByVal sStore As String, ByVal Basket As Fruit = BasketEmpty) As DWORD
 Print Basket.Banana

Pierre Bellisle

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 57
Re: Function with optional UDT parameter
« Reply #3 on: December 22, 2016, 05:21:14 PM »

Superb,

#define BasketEmpty will be great for now, being enable to initialize give more power.

"= TYPE()" seems also pretty good and versatile...

and

"PTR = NULL" will be surely very usefull some time later.

I really like FB more and more.

Thank you Josť, great answer. :-)

Pierre
Logged

Pierre Bellisle

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 57
Re: Function with optional UDT parameter
« Reply #4 on: December 22, 2016, 05:36:53 PM »

A potpourri made with Josť's recipies...

Code: [Select]
'Console
#Define unicode
#Include Once "windows.bi"

Type Fruit
  Banana     As WORD
  Clementine As DWORD
  Coconut    As Double
  Lime       As WORD
  Lemon      As String * 15
End Type

Common BasketEmptyCommon As Fruit '=> {2, 5, 1.5, 2} 'COMMON variables cannot be initialized

#Define BasketPreFilledDefined Type(7, 22, 1.1, 3, "Yellow")

Function DoGroceryPointer(ByVal sStore As String, ByVal Basket As Fruit Ptr = NULL) As DWORD

 If Basket Then
  Print "[" & Basket->Banana & "]"     
  Print "[" & Basket->Lemon & "]"
 Else
  Print "[No banana]"     
  Print "[No lemon color]"
 EndIf
   
End Function

Function DoGroceryType(ByVal sStore As String, ByVal Basket As Fruit = Type(2, 5, 1.5, 2, "Light yellow")) As DWORD

 Print "[" & Basket.Banana & "]"
 Print "[" & Basket.Lemon & "]"
   
End Function

Function DoGroceryCommon(ByVal sStore As String, ByVal Basket As Fruit = BasketEmptyCommon) As DWORD

 Print "[" & Basket.Banana & "]"
 Print "[" & Basket.Lemon & "]"

End Function

Function DoGroceryDefine(ByVal sStore As String, ByVal Basket As Fruit = BasketPreFilledDefined) As DWORD

 Print "[" & Basket.Banana & "]"
 Print "[" & Basket.Lemon & "]"
 
End Function

Dim Basket As Fruit

Basket.Banana = 22
Basket.Lemon  = "Green"

Print "#Define"
DoGroceryDefine("Store", Basket)
DoGroceryDefine("Store")
Print

Print "Common"
DoGroceryCommon("Store", Basket)
DoGroceryCommon("Store")
Print

Print "Type"
DoGroceryType("Store", Basket)
DoGroceryType("Store")
Print

DoGroceryPointer("Store", @Basket)
DoGroceryPointer("Store")

Print "Press a key or click to end"
Dim buttons As Long
Do
  GetMouse(0, 0, 0, Buttons) : If buttons Or Len(Inkey) Then Exit Do : Sleep 200   
Loop
« Last Edit: December 22, 2016, 09:10:50 PM by Pierre Bellisle »
Logged

Josť Roca

  • FireFly3 Registered User
  • Master FireFly Member
  • *
  • Posts: 2785
    • Josť Roca Software
Re: Function with optional UDT parameter
« Reply #5 on: December 22, 2016, 06:14:30 PM »

If you want to type less, instead of

Code: [Select]
DoGrocery4("Store", VarPtr(Basket))

you can use

Code: [Select]
DoGrocery4("Store", @Basket)

Contrarily to PowerBASIC, @ does not deference a pointer (FB uses * for that), but it is a shortcut for Address Of and can replace both VARPTR and PROCPTR. It even works with string literals.
« Last Edit: December 22, 2016, 06:18:57 PM by Josť Roca »
Logged

Pierre Bellisle

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 57
Re: Function with optional UDT parameter
« Reply #6 on: December 22, 2016, 07:34:06 PM »

Yep, I forgot to use @, this was an old reflex, code corrected.

Thank,
Pierre
Logged