method and function
BlitzMax Forums/BlitzMax Beginners Area/method and function
| ||
OO so confuse. What the different between method and function? anyone.. thanks |
| ||
I think I am right in saying that as far as types are concerned, a method has implicit access to the data in the type but a function does not. This means that if you call a function related to a type you must still pass an explicit reference to the the type instance concerned. an example of a method and a non working function... Type mytype Field x,y,z Method do_method() x=1 y=2 z=3 End Method Function do_function() x=4 y=5 z=6 End Function End Type a:mytype =New mytype a.do_method() Print a.x+" "+a.y+" "+a.z a.do_function() Print a.x+" "+a.y+" "+a.z WaitKey() End You see how the call to do_function() failed to change the data in the type. An example of how to get the function working... Type mytype Field x,y,z Method do_method() x=1 y=2 z=3 End Method Function do_function(thistype:mytype) thistype.x=4 thistype.y=5 thistype.z=6 End Function End Type a:mytype =New mytype a.do_method() Print a.x+" "+a.y+" "+a.z a.do_function(a) Print a.x+" "+a.y+" "+a.z WaitKey() End That, to the best of my knowledge is the difference. |
| ||
Right. Also, you can call the function from the type itself - mytype.do_function() - instead of calling it from an instance. |
| ||
@ Shambler An example of how to get the function working... Thanks for this example. It really helps beginners like me.@ denzilquixode Also, you can call the function from the type itself - mytype.do_function() - instead of calling it from an instance. Would you mind posting a complete working example of this to help anyone trying to learn this stuff. Thanks |
| ||
Just change the linea.do_function(a) in Shambler's second bit of code to mytype.do_function(a) That's it. Think of functions as a property of the type itself, rather than specific objects of the type. Do you understand the use of Globals in types? It's a very similar idea. |
| ||
Oh, now I understand the difference. I didn't really get why I should use Methods instead of internal Functions. But "property of the type itself" put my head into place, thanks. |
| ||
Now i am confused. In what situation a function would be more handy then a method inside the type? |
| ||
I guess operations that manipulate multiple objects in one condition. This is kind of what I mean:If bullet1:Bullet CollidesWith enemy4:Enemy enemy4.hp:-4 player1.score:+1 EndIfPseudo-code, of course. Not sure if this is right. Someone will have to varify... |
| ||
To clarify things it is best to see a method as standard function that gets an additional implicit parameter : the "Self" parameter that references the object. So if you don't need any access to the object, use a simple function and not a method (note that this is not entirely the same, as the Self parameter is really treated specially, but that is not relevant here). But then you may ask why you'd ever need to have a normal function inside a type, and why not simply put it in the global scope? The first reason is for documenting purpose, and to make things more logical. Typical example: a "Create" function that creates an object with some parameters: Type MyType Field f1% Field f2$ Function Create.MyType(param1%, parm2$) Local obj:MyType = New MyType obj.f1 = param1 obj.f2 = param2 Return obj End Function End Type ... Local o.MyType = MyType.Create(5,"blabla") Isn't it logically neater to have such a "Create" function in each type, rather than to create functions like CreateMyType2, CreateMyType2 etc. Second point (correct me if I'm wrong, this is from memory - from the beta version), you can have private member functions, just like private fields or methods. This is something that you could hardly do if the function were not part of the type itself. |
| ||
Thank you for that nice explanation, it IS handy. |
| ||
Right. Also, you can call the function from the type itself - mytype.do_function() - instead of calling it from an instance. Which is how you should do it in the first place. In what situation a function would be more handy then a method inside the type? In situations where you don't have, or don't need a specific object/instance. For example when dealing with creation problems (as mentioned above) and access to "Global fields" (also know as static variables, class variables or type variables) in the type.Ofcourse in Blitz, there are no access modifiers and thus everything has global, or near-global visibility, thus somewhat limiting its usefulness. |