Passing a variable to a function

Blitz3D Forums/Blitz3D Programming/Passing a variable to a function

mrtricks(Posted 2005) [#1]
I wondered if it was possible to pass a variable itself to a function rather than the contents of a variable. For instance: If you have a type with lots of different fields, and you want a function to find a certain type instance, but at different times you might want to search using different fields...
Type stuff
Field x, y, z, strength
End Type

Function search.stuff( <var>, value )
For s.stuff = Each stuff
If s\<var> = value Then Return s
Next
Return Null
End Function

...then...
thing.stuff = search.stuff( <x>, 5 )

... finds the .stuff whose x=5, and...
thing.stuff = search.stuff( <strength>, 100 )

... finds the .stuff whose strength=100.

ATM you can do this only by having a different function for searching each different field.

Even better if it could handle different types of variable, so...
thing.stuff = search.stuff( <name$>, "Alan" )
thing.stuff = search.stuff( <weapon.weapon>, smg.weapon )

... was legal. EVEN BETTER if you could have a type function that could work on any type, so...
Function search.<type>( <field>, value )

thing.stuff = search.stuff( <name$>, "Alan" )
car.vehicle = search.vehicle( <buildyear>, 1990 )

EVEN BETTER if you could have optional extra variables in a function, so...
Function search.[type]( <field1>, value[, <field2>, value2] [, <field3>, value3] )

Wouldn't that be super?


Stevie G(Posted 2005) [#2]
The only way I can see you doing this is to define constants which act as indexes for the fields within the type .. sort of like this ..

const I_x = 1
const I_y = 2
const I_z = 3
const I_Strength = 4

Search( I_Strength , 100 )

function search.stuff( field , value )

for s.stuff = each stuff
select field
case I_x
If s\x = value return s
case I_Strength
If s\Strength = value return s
etc.....
end select
next

return null

end function


mrtricks(Posted 2005) [#3]
Ugh. It'd work, but it puts me off my spaghetti. ;)


RGR(Posted 2005) [#4]
Whenever you need stuff that is not supported, think if you can solve it with banks.
Types are a description how much memoryspace is needed, for the fields, for links to next and previous type, links to Strings, etc.
Costs some hours of work and you have your own Types, Variables, Arrays, whatever you need.
Another advantage is, that you can custmize everything the way YOU need. For instance if you need Sorting you can sort your TypeList by any Typefield without indexarrays only by swapping pointers...
You know the old Blitz Slogan? The only limit is your imagination...


Stevie G(Posted 2005) [#5]

Ugh. It'd work, but it puts me off my spaghetti. ;)



I know what you mean ... completely hideous!!


mrtricks(Posted 2005) [#6]
I hear you RaGR, but part of the appeal of Blitz to me is having a succinct command set. Otherwise I'd learn C++. I'm not that hardcore yet...