function and methods

BlitzMax Forums/BlitzMax Programming/function and methods

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

defining a Function inside a type is just a way of associating that function with that type, and saving namespace.

For example:
Function finddude(name$)
	For d:dude=EachIn dudes
		If d.name=name Return d
	Next
End Function

Global dudes:TList
Type dude
	Field name$
End Type


is functionally the same as:
Global dudes:TList
Type dude
	Field name$
	
	Function find(name$)
		For d:dude=EachIn dudes
			If d.name=name Return d
		Next
	End Function
End Type


but now you can have lots of types each with a Find function, instead of making finddude, finddog, findtree etc.


Now, methods are fucntions that deal with a particular instance of a type, and allow you access to the instance's fields in the top namespace.

Function changedudename(d:dude,newname$)
	d.name=newname
End Function

Type dude
	Field name$
End Type


is the same as:
Type dude
	Field name$
	
	Method changename(newname$)
		name=newname
	End Method
End Type


but you don't have to pass in the dude you're using as a parameter, and you can write just 'name' instead of 'd.name'.