Ok, what is the REAL difference between method....

BlitzMax Forums/BlitzMax Programming/Ok, what is the REAL difference between method....

Russell(Posted 2005) [#1]
Ok, I heard someone mention that the difference between methods and functions (in this case refering to using them within a UDT) was that functions return a value and methods don't. But that doesn't seem to be the case. Or is it?

If they are the same, then no biggy (although one might say "Why have both if they are the same?").

Just thought I'd ask to get a 'definitive' answer.

Russell


Perturbatio(Posted 2005) [#2]
This thread may help you understand them better:
http://www.blitzbasic.com/Community/posts.php?topic=41790#469155


Russell(Posted 2005) [#3]
Thanks, Perturbatio!

Russell


Bot Builder(Posted 2005) [#4]
Functions are specific to the type decleration, while methods are specific to a type instance:

Type Car
 Field Name$

 Method Honk()
  Print Name$+" honks horn"
 End Method

 Function Create:Car()
  Local tmp:Car=New Car
  return tmp
 End Function
End Type

MyPorsche:Car=Car.Create()
MyPorsche.Name="Porsche"
MyPorsche.Honk()


[edit]Argh cross post :|


Perturbatio(Posted 2005) [#5]
np :)

That's weird, I could've sworn skid posted in here...

*CUE TWILIGHT ZONE MUSIC*


Russell(Posted 2005) [#6]
So, could it be said basically that methods() are 'local', while functions() are 'global'? And further, new memory is allocated for each Method when a new instance of an object is created, whereas only one copy of a function needs to occupy memory and they all use that?

Thanks for the link! (Hope this discussion makes it into the manual :/)

Russell


Jeremy Alessi(Posted 2005) [#7]
Methods are cooler when you want to change something just on an instance of a type. Also within the method you don't need a variable to refer to that instance you just refer to the variables as Self.x etc... Most of the time you should use methods if an instance exists already.


AaronK(Posted 2005) [#8]
So, could it be said basically that methods() are 'local', while functions() are 'global'? And further, new memory is allocated for each Method when a new instance of an object is created, whereas only one copy of a function needs to occupy memory and they all use that?


No it couldn't be said. :)

Methods will have only one copy of the code, just like functions. Even if you make 1 million instances (objects) of a Type.

Basically, Functions cannot access Fields nor Methods in a Type, because they are not working with an Object. They can access other Functions, Const's and Global's of a Type though because none of those actually require an object to work.


Aaron


FlameDuck(Posted 2005) [#9]
So, could it be said basically that methods() are 'local', while functions() are 'global'?
In Blitz terminology, maybe.

A more accurate approach is to describe them in general OO terms. Thus a method is associated with an object or instance, while a function (or static method) is associated with a type or class.

Generally a static method can only access static fields, thus if one was to extend this to BlitzMAX terminology it could be said that since Functions can only access Globals then they are like Global methods. I don't agree with adding this level of terminology confusion, but I accept the argument that it might be easier for programmmers more accustomed to procedural programming to grasp things in terms of scope, rather than objects.

And further, new memory is allocated for each Method when a new instance of an object is created, whereas only one copy of a function needs to occupy memory and they all use that?
No. The difference here is that a method includes the object it was invoked from (self / this) as an implicit parameter.