Method & Functions in Types

BlitzMax Forums/BlitzMax Beginners Area/Method & Functions in Types

RetroRusty(Posted 2006) [#1]
Can someone tell me what the difference is between a Method and a Function in a type? Sorry if this is really easy but not done that much in MAX.

Thanks


(tu) sinu(Posted 2006) [#2]
Method has to be used on a instance of a type and a function doesn't e.g


RetroRusty(Posted 2006) [#3]
Can you give me a sample of what you mean?

Thanks


H&K(Posted 2006) [#4]
When you call a Method, it is called by a specific Instance of a type

AnInstance.AMethod ()

When you call a Function, it is called by the type as a whole

TheType.AFunction ()

A good example would be Create(), which is quite a standard FUNCTION, it needs to be a function because the actuale instance doesnt exist until after create has run.


Often you will find that you can use either

MyFunction (AnInstance, paramone,paramtwo)

or

AnInstance.MyMethod (ParamOne,ParamTwo)


RetroRusty(Posted 2006) [#5]
So a Method only changes an instance of a type where a function will change every instance of that type?


H&K(Posted 2006) [#6]
A method, can if it wants too change other instances of the type. But when it is called it is called by a specific instance. (self)

Normaly you use Method to mess with one (Or mayby two) instances and function to mess with the whole type.
Function will not change every instance of that type, (It can if you want it to), but normaly you use it to either create, or to change any type-globals.


RetroRusty(Posted 2006) [#7]
OK, thanks for your help :)


The r0nin(Posted 2006) [#8]
The biggest advantage of a method (IMHO) is that it works with all of the fields of an instance automatically, where a function has to be passed the specific instance you want it to work on. For example:

Type TThing
    Field x:Int
    Field y:Int

Method SetVals(x_in:Int,y_in:Int)
    x = x_in
    y = y_in
End Method

Function ValSet(thing_in:TThing, x_in:Int, y_in:Int)
    thing_in.x = x_in
    thing_in.y = y_in
End Function

End Type

Now, using the above code, there are two ways to set the x and y fields of a specific instance of TThing. Let's make one and set the x and y using a method:
thing:TThing = New TThing
thing.SetVals(4,5)

Note that using the method, you simply send in the values you want and put them in the fields. At any point and time, I can simply call the method and change the values. Now let's do it the other way:
thing:TThing = New TThing
TThing.ValSet(thing,4,5)

Notice that this time I have to let the function know what Type I'm calling the function from (ValSet.TThing), and I'm having to pass what object I'm talking about into the function in addition to the values (thing,4,5). Also, note in the Type code how I have to specify which object's x field I'm taking about (thing_in.x = x_in), whereas the method simply uses the fields of the object that called it (x = x_in)

In most cases, I use methods, just because they are easier to code and they make sure that the only changes being made are to the object that is calling it (unlike functions, which can be called by anything and change anything).

Hope this helps!