Imitate Procedures/Methods in Blitz
Blitz3D Forums/Blitz3D Beginners Area/Imitate Procedures/Methods in Blitz
| ||
Hi there, anyone familiar with Java or Pascal will know of Methods and Procedues and how one can pass in and retrieve more then one variable from them. Functions however can only 'return' a single variable which is somewhat of a limitation in certain circumstances. Is there any other way to imitate methods or procedues in Blitz, I would like to send a function a few parameters, have them modified and receive more than one back again. One solution I have thought of but not yet implemented due to it's long windedness is to concatenate all results in the function into a string, return that and then split it back up to retrieve all seperate values, but, is there any simpler way? |
| ||
Something like this?Type TCoord Field x,y End Type example.TCoord=ExampleFunc(2,10) Print example\x Print example\y Function ExampleFunc.TCoord(x,y) e.TCoord=New TCoord e\x=x e\y=y Return e End Function |
| ||
OK, sure, that is possible too, but it's not convenient to turn everything into an array or a type just so you can pass 2 or more variables into a function and have them returned, it's just not the same as a procedure or a method. Maybe it isn't possible with Blitz without going the extra mile... |
| ||
Just break your procedure down into its smallest components and create a function for each component. Here is an example... Procedure Point_Relative(x,y,OffsetX,OffsetY) ; Returns New coordinates for a point on the screen ; relative to X & Y NewX=x+OffsetX NewY=y+OffsetY Return NewX,NewY End Proc You obviously cannot do the above in Blitz so split it into its smallest components which is 2 because we want 2 new values. If you wanted 3 new values you would have to create 3 functions. Example below.... Function Point_RelativeX(x,OffsetX) NewX=x+OffsetX Return NewX End Function Function Point_RelativeY(y,OffsetY) NewY=y+OffsetY Return NewY End Function Then all you do is call both functions to get both values. i.e. X=Point_RelativeX(GraphicsWidth(),10) Y=Point_RelativeY(GraphicsHeight(),10) Text X,Y,"Hello their dude!" The above will display 'Hello their dude!' in the centre of the current screen just slightly 10 pixels right and 10 below the absolute centre. Hope that helps! Regards |
| ||
Ooops! a slight mistake in the above code. GraphicsWidth()/2 and GraphicsHeight()/2 Sorry! Regards |
| ||
Er, umm, any of you guys ever figure on using the GOSUB/RETURN commands? There is no need for parameter passing, as it uses the same variables as the main program. This way, you can uses whatever number of variables you want. |
| ||
You can also use global variables which just get set by the function... so it doesn't return anything. Basically, there isn't a way for Blitz to return more than one variable... probably because it's the same way in the language Blitz was programmed in, and it wasn't necessary to change it. If you really wanted to, returning a type is the way to go. And that would be a lot simpler than concatenating them into a string... Alternatively, you could make a "custom" type and use bit masking and shifting to get the values you want... once again, harder than using a type (or globals... but "cleaner" than globals) |
| ||
If you put all the parameters you want to return into a type then you can do it. Bit more fiddly as you have to create/delete an instance of the type but as long as you follow your own coding rules it should work. |
| ||
OK guys, thanks for the input, as I thought, blitz has no procedure or method support so one has to work around the problem. Breaking it up into many small function works but one can end up with a lot of functions in that way, nonetheless, it works. Sure, I'm aware of Gosub but I learned it's not good programming practise, code should flow, not jump around. Also, while one can use global variables they take up more memory which although might not be substantial to cause a problem is again not good practise if they can be passed to functions. Anyway, thanks again, will use a mixture of multiple functions, global variables and types to get the job done. |
| ||
I've toyed with making one big custom type which contains all the parameters I'll ever want to pass, instantiating it with "Global p.Params = New Params", and then just using p\variablename whenever I pass things around. It's sort of like writing a C++ header in that you can just dump new fields into the big type declaration whenever you need to. Gosub used to be faster than functions in Blitz. I dunno if it still is. |
| ||
Zasy wrote: Sure, I'm aware of Gosub but I learned it's not good programming practise, code should flow, not jump around. Who says it's not good programming practice? Functions and Gosubs are VERY similar, and each has advantages and disadvantages: Function: advantage: 1) Can be fed as many parameters from the main program as needed. 2) Isolates variables within from those in main program. disadvantage: 1) Only one value can be ejected (returned) from it. 2) Isolates variables within from those in main program. Gosub:1) Can be fed as many parameters from the main program as needed. 2) It returns back to the main program as many values as you need. 3) Uses the same variables as the main program. disadvantage: 1) Uses the same variables as the main program. As I see it, Functions have their use, and Gosubs do, also. You will also note that for each of the above, an advantage has been listed that can also be a disadvantage, depending on your programming style. |
| ||
Zasy wrote: Sure, I'm aware of Gosub but I learned it's not good programming practise, code should flow, not jump around. Maybe you heard that using a lot of goto's is not good programming practice. |
| ||
Aaaaand we're off... |
| ||
I haven't used a GOSUB since my Spectrum days... |
| ||
Well, they tend to fit fine for the function structure that some programmes like to use (not my style so I don't use gosubs very often). What was that he said about globals taking more memory? |
| ||
GOSUB's are functionally pointless (same damn thing as two GOTO's). Fa'geddaboudem In all honesty, a GOTO is more useful than a GOSUB. |
| ||
Zasy, is this of any use. http://www.blitzbasic.com/codearcs/codearcs.php?code=1097 kev |
| ||
You can pass Blitz Arrays into functions (or even Blitz Arrays in Types).Local a[3] a[1] = 20 Print a[1] DoThing(a) Print a[1] WaitKey End Function DoThing(b[3]) b[1] = 45 ;do stuff End Function |