Difference between function and method

BlitzMax Forums/BlitzMax Beginners Area/Difference between function and method

qim(Posted 2009) [#1]
Can someone please explain to me what's the difference between functions and methods in types? Both seem to work, but not always. And I'm not sure, why. :-/


Sledge(Posted 2009) [#2]
Doing my best to put it in layman's terms, a function is something the type 'does' whereas a method is something an instance of the type 'does'.

EDIT: Here's an example...


Notice how the list is global? That means it belongs to the type rather than any particular instance of the type, so it can be thought of as existing at the same 'level' as the functions. The first function populates the list with a bunch of instances, the second function iterates through each instance on the list and tells each one to first move itself then draw itself.

Of course you could organise things differently. UpdateAll() could just as easily look like this...

	Function UpdateAll()
		For Local currentStar:star = EachIn list
			currentStar.xPos = currentStar.xPos - currentStar.vel
			If currentStar.xPos<0 currentStar.xPos:+GWIDTH
			SetColor currentStar.col,currentStar.col,currentStar.col
			Plot currentStar.xPos, currentStar.yPos		
		Next
	End Function

...which negates the need for any methods at all. So why have them? Well it gives you more flexibility to organise your code and makes it possible for types to inherit behaviour from other types in a modular way.


Czar Flavius(Posted 2009) [#3]
A method acts on a particular object, and can access its fields. A function within a type does not have automatic access to any object. Most of the time you could put a function outside of a type, and it will work the same. They are often put inside the type simply to help better organise your code - so that functions which are related to a particular kind of object are together.

In this simple example, we have a creature type, and creatures can fight each other, and take damage. Creatures will have as fields hitpoints, attack power, and a chance of doing double damage. They will have a method that works out how much damage to inflict, taking into account the bonus chance. They will have a method to take damage, which will reduce their hitpoints, and a method to die whose code I'm not writing (it depends how the creatures are stored, in a list/array etc)

How to battle them? You can do it with a function or method - I've shown both ways.

Type TCreature
	Field hitpoints:Int, attackpower:Int, chance:Int
	
	Method attack:Int()
		Local roll:Int = Rand(0, 100)
		If roll <= chance
			Return attackpower * 2
		Else
			Return attackpower
		End If
	End Method
	
	Method takedamage(damage:Int)
		hitpoints :- damage
		If hitpoints <= 0 Then die()
	End Method
	
	Method die()
		'......
	End Method
	
	Function battle(attacker:TCreature, defender:TCreature)
		Local punch:Int
		punch = attacker.attack()
		defender.takedamage(punch)
	End Function
	
	Method battle(victim:TCreature)
		Local punch:Int
		punch = attack()
		victim.takedamage(punch)
	End Method
End Type

'let's say we have two TCreatures, dragon and panther

'to make dragon attack panther using function
TCreature.battle(dragon, panther)

'to make dragon attack panther using method
dragon.battle(panther)


If you change attack() to a function, it won't work, because when it tries to access attackpower, it doesn't know which object it's associated with. Likewise if you make takedamage() a function, it won't know which creature it is supposed to reduce the hitpoints of.

The battle function is given both creature objects it needs to use. The battle method only needs to be given one object, as when you call it in the code, the method will already be associated with an object (dragon in this case) and so can access dragon's fields and methods automatically. So in Method battle we can just use attack() directly, whereas in Function battle we must specify with attacker.attack() which object we are talking about.

Note - you can't have a method and function within a type of the same name, so to use the example code you'd need to delete either the method or the function, according to which you prefer. (Or rename one of them)

I hope this clears it up a bit for you.


_Skully(Posted 2009) [#4]
A function within a type is no different than a function at the root. Its a way of nicely grouping functions specific to a Type that will follow should you move code around between projects.

Methods on the other hand, like said above, only work following instantiation (new)


qim(Posted 2009) [#5]
Wow, thanks. :-)


Beaker(Posted 2009) [#6]
Using Self makes things a little clearer (optional). Like this:
Method takedamage(damage:Int)
	Self.hitpoints :- damage
	If Self.hitpoints <= 0 Then Self.die()
End Method