Function Parameters
BlitzMax Forums/BlitzMax Beginners Area/Function Parameters
| ||
I'm a little confused here, I see this from time to time, a function defined with parameters but being called with no parameters eg: Function blah(x:int,y:int) end function something = blah |
| ||
I think that's picking up the function pointer, not actually calling the function. You just need the identifier of the function to grab it's pointer. |
| ||
Right, that code there is assigning a function pointer only, thats in this case obvious because you assign it to something without having () which means that the function can not return anything What you though also might see are cases like Function blah(x:int = 0, y:int = 0) ... end function blah ... in which case just using blah is fine for two reasons: 1. it does not need parameters due to the defaults 2. it does not return anything so () can be omited as well (though not really supporting the readability) |
| ||
Ah pointers?! What are they? any tutorials/references? |
| ||
To put it in simple talk, you can store functions in variables. It's used mainly for advanced programming so it's hard to give an example that couldn't be done better another way.Strict Function mean:Int(score:Int) Print "Inside mean" Return score End Function Function generous:Int(score:Int) Print "Inside generous" Return score * 2 End Function Local scoring_function:Int(a:Int) scoring_function = mean Print scoring_function(10) 'prints 10 scoring_function = generous Print scoring_function(10) 'prints 20 Inside mean 10 Inside generous 20 Notice the special variable type of the scoring_function variable. It has not just an Int like normal, but a bit in brackets. The bit just after the colon is the return type of the function, and in the brackets you put your parameters. Rule: A function must match the return type and parameters of the function pointer (scoring_function) EXACTLY. Rule: you must give a name to the parameters of the function pointer, but they DON'T need to match. I purposely called it something different to show this. |
| ||
Very cool, I get it! Are function pointers common usage? Are there times when they are extremely helpful? |
| ||
they are pretty common for things like AI, Finite State Machines and efficient gui implementations / event handlers, there is nothing that can replace them realistically |
| ||
However for simpler things they are more a novelty than a necessity :) You use them without knowing when you use a custom sort function. In this code I haven't used the private fields, setters and getters I extolled the virtues of in the other thread as I am in a hurry. Making those changes to the code is left as an exercise to the reader :) Hint: think what Asserts you could use too. SuperStrict Type TScoreRecord Field player:String Field score:Int Function Create:TScoreRecord(player:String, score:Int) Local record:TScoreRecord = New TScoreRecord record.player = player record.score = score Return record End Function 'due to Blitz requirement, it must always take two Objects and not two anything else 'it returns 1 if the first object is greater than the second 'it returns -1 if the first object is less than the second Function compare_scores:Int(o1:Object, o2:Object) 'convert these objects to score records now Local r1:TScoreRecord = TScoreRecord(o1) Local r2:TScoreRecord = TScoreRecord(o2) 'it's possible the list contains objects that are not TScoreRecord 'we test if an object is not a TScoreRecord and return safely If (Not r1) Or (Not r1) Then Return 0 'the actual comparison takes place here If r1.score > r2.score Then Return 1 If r1.score < r2.score Then Return -1 Return 0 End Function End Type Local list:TList = New TList For Local n:Int = 0 To 10 list.AddLast(TScoreRecord.Create("Player " + (n+1), Rand(10, 100)*10)) Next For Local record:TScoreRecord = EachIn list Print record.player + " has a score of " + record.score Next Print "" Print "" Print "" 'use our custom sorting function now. so far this looks like a tutorial on sorting 'BUT THIS STEP USES A FUNCTION POINTER! WOOOOOOO list.Sort(False, TScoreRecord.compare_scores) For Local record:TScoreRecord = EachIn list Print record.player + " has a score of " + record.score Next Man, I'm awesome! |