Functions - Accepting Optional Arguments By Ref
BlitzMax Forums/BlitzMax Beginners Area/Functions - Accepting Optional Arguments By Ref
| ||
I know you can have optional arguments, i.e. Function PrintSomething(a, b = "aaa") EndFunction so that b is assigned 'aaa' if no argument is passed, but is it possible to have optional arguments passed by reference? So for the following function, can I call it by using GetPixelColor (100,100) and have the function just return the pixel color or call it using GetPixelColor (100,100,a,r,g,b) to get the color back in it's alpha, r, g & b values? Function GetPixelColor:Int(x:Int, y:Int, a: Int Var, r: Int Var, g: Int Var, b: Int Var) ' Code sources: ' http://blitzbasic.com/Community/posts.php?topic=79641#894234 ' http://www.blitzbasic.com/Community/posts.php?topic=56368#626910 Local col:Int Local mypix:TPixmap=GrabPixmap(x,y,1,1) col = ReadPixel(mypix,0,0) a = ( col & $ff000000) r = ( col & $ff0000 ) Shr 16 g = ( col & $ff00 ) Shr 8 b = ( col & $ff ) Return ReadPixel(mypix,0,0) EndFunction |
| ||
If you don't pass anything in that slot, what would the default be a reference to? There would be no safe way to use it. |
| ||
You can pass a type with all the fields you need instead. Default to Null to make it optionalSuperStrict Type TColor Field a:Int Field r:Int Field g:Int Field b:Int End Type Function GetPixelColor:Int(x:Int, y:Int, Color:TColor = Null) ' Code sources: ' http://blitzbasic.com/Community/posts.php?topic=79641#894234 ' http://www.blitzbasic.com/Community/posts.php?topic=56368#626910 Local col:Int Local mypix:TPixmap=GrabPixmap(x,y,1,1) col = ReadPixel(mypix,0,0) If Color Color.a = ( col & $ff000000) Shr 24 Color.r = ( col & $ff0000 ) Shr 16 Color.g = ( col & $ff00 ) Shr 8 Color.b = ( col & $ff ) End If Return ReadPixel(mypix,0,0) EndFunction Graphics 640,480 SetColor 100,200,255 Plot 10,10 Print GetPixelColor(10,10) Local Color:TColor = New TColor GetPixelColor(10,10,Color) Print Color.a+" "+Color.r+" "+Color.g+" "+Color.b |
| ||
If you don't pass anything in that slot, what would the default be a reference to? There would be no safe way to use it. Well having a default is just the way of making an argument optional, unless there is another way I'm missing. I want the code to either just return an INT specifying the color of the pixel or return an INT as well as the color breakdown into alpha, r, g & b if needed. If I can do it with types is fine, the other way is to just write another function which converts a color INT into the Alpha, r, g & b values I guess. |