oop questions .. from a beginner

BlitzMax Forums/BlitzMax Beginners Area/oop questions .. from a beginner

hugblue(Posted 2012) [#1]
hi all ..
just started with oop and is reading a beginners guide.

my question is about function vs method.
as i understand this :
Functions is outside Type
and Methods is inside Types
or maybe i am incorrect in this.

can anyone describe in an easy to understand :

what is the difference between functions and methods.
what is the objective use for them both separated.
how do they differ in how they are used.

i need to get my head around this and need to understand the logic.

please supply with examples and text.

Last edited 2012


matibee(Posted 2012) [#2]
Functions are not tied to an instance of the type, same for globals. Fields and methods are.

Type thing
	
	Global XSTART:Float = 5
	
	Field name$
	Field x:float
	
	Function MakeThing:thing( name$ )
		Local t:Thing = New thing
		t.name = name
		t.x = XSTART
		Return t
	End Function 
	
	Function SetXStart( x:Float )
		XSTART = x
	End Function 
	
	Method Move( distance:Float )
		x :+ distance
	End Method 

End Type 




Last edited 2012


hugblue(Posted 2012) [#3]
can you explain instance.

and your text is extremely compact. a little tooo compact.

Last edited 2012


matibee(Posted 2012) [#4]
It's an instance (in that example) of "thing". An actual living breathing object you can poke with a virtual stick..

instance1:thing = thing.MakeThing( "bob" )
instance2:thing = thing.MakeThing( "fred" )

instance1.move( 20 )
instance2.move( 99 )



hugblue(Posted 2012) [#5]
hi matibee ..

first. i am born danish not english. i can read english pretty well and tal and understand alot of it. thats why i need to have it explained more detailed to get my head around the english ways and expressions. when you explain it so simple. it really makes it some harder for me.

because i am not born english. and that the understanding. of some terms in oop and english in general. do not fall naturally for me.

so plz. explain. more detailed. thz.

i just want to learn and understand. smiling.

Last edited 2012


Yasha(Posted 2012) [#6]
Both functions and methods exist "within" the type's scope (that much you can see from the way it's written).

Variables declared as "global" within the type only exist once. They are essentially the same as a global somewhere else, except that they happen to be better organised by being associated with the rest of the related stuff. Variables declared as "field" form part of each object - each instance of the type - that is created by calling "New thing". If you call "New" three times ("a = New thing; b = New thing; c = New thing"), three objects have been created and there are three sets of the instance variables (fields) "name" and "x", but all three share the static variable (global) "XSTART". If one sets it, the change is visible to the others.

Functions and methods are analogous to this. Functions are essentially the same as functions declared outside the type definition, as with globals. Since they aren't attached to a specific instance, they can't see instance variables (fields), only static ones (globals). They are usually used when you want to perform an operation that affects the static parts of the type (i.e. all instances), or when you want a custom object creation more interesting than just "New" (since making a new instance means you can't send a message to an existing instance).

Methods on the other hand operate like fields: conceptually, each instance gets its own ones and they exist within that object's scope. Therefore, they can see that object's fields, and modify them. The methods of the same name on two different objects exist in two different scopes; once can see the fields of the first object and one of the second object. This means that a method of object A will not necessarily do the same thing as a method with the same name of object B, as they are each affected by a different set of variables that they can see and modify. (Later you will learn that they can also have different executable code, which extends this same concept - that methods belong to individual objects - significantly.)

In other languages (if this helps you), these two concepts are referred to as "static methods" and "instance methods" respectively, because the first only operates on the static parts of the type-as-a-whole (it belongs to the whole type, or is shared by all instances of it), whereas the second operates on individual instances (it belongs to only one instance, and cannot affect others directly).

Last edited 2012


matibee(Posted 2012) [#7]
When you define a Type, no actual instances of that type exist yet. The Type code is a just a box for holding fields and/or methods.

Type thing
  field x:Float
  field name$
End Type


That's the basic type from above. "thing" is just something we want to have a floating point x value and a name.

When you create an instance of "thing" a physical memory area is set aside for it to exist in.

instance1:thing = New thing


Now instance1 refers to the memory area where "thing" lives.

If you want to perform an operation on a specific instance of "thing", then you can supply a built in method:

Method move( distance:Float )
  x :+ distance
end method 


A Method will work on the fields of the calling instance...

instance1.move( 20 )


A non OOP way of doing the same is to write an external (ie, away from the type) Function..

Function MoveThing( athing:Thing, distance:Float )
   athing.x :+ distance
end function 


Don't confuse this with Type Functions! More on them later..

now we have a type that looks like this..

Type thing
  Field name$
  Field x:float
	
  Method Move( distance:Float )
     x :+ distance
  End Method 

End Type 


Ok so far?


hugblue(Posted 2012) [#8]
super super.

thz both.

really like the explanations.

super.


hugblue(Posted 2012) [#9]
so if :

Type thing
  Field name$
  Field x:float
	
  Method Move( distance:Float )
     x :+ distance
  End Method 

End Type


is the same as :

Type thing
  Field name$
  Field x:float
End Type

Method Move( distance:Float )
  x :+ distance
End Method


or is it two different things ?
and is not 100% sure of functions and methods. but wil reread this post again and again till i understand it. but. i understand what they can do. but not directly the difference.


matibee(Posted 2012) [#10]
Ok :)

A lot of Types have multiple fields that must be initialised before the instance is of any use.

We could:
instance1:thing = new thing
instance1.x = 5
instance1.name = "bob"


But repeating that for every new instance will get tedious. And if we add or remove fields you'll have to go over all your code and modify it.

The Blitzmax way of performing initialisation is to add a specific function to the type that returns a new instance, all nicely initialised for us:

Function MakeThing:thing( name$ )
  Local t:Thing = New thing
  t.name = name
  Return t
End Function 


so now a new and initialised instance is easier to create:
instance1:thing = thing.MakeThing( "bob" )


Notice how the MakeThing function is called by putting "thing." in front of it.


Yasha(Posted 2012) [#11]
or is it two different things ?


Different things.

Firstly, since methods are "owned" by an instance of the type where they are defined, that method is not longer "owned" by any instances of type thing - you have removed it from the type.

Secondly, that method isn't defined within another type, so it isn't owned by anything and is therefore not valid BlitzMax any more.

Your first example is very loosely equivalent to this:

Type thing
  Field name$
  Field x:float
End Type

Function Move( this:thing, distance:Float )
  this.x :+ distance
End Function


Functions aren't "owned" by objects and therefore if you want one to manipulate an object, the object has to be passed in manually. Methods get access to their object automatically.

Note that the above example is only equivalent to your first code block in very simple cases, because it presumes from the outside what you want to happen when you send the "Move" message to a "thing". Because methods are "owned" by an object, it's entirely possible for the "thing" in question to actually run a completely different method body depending upon which implementations it owns (this is called "polymorphism", and is ~99% of why OOP is useful - but you'll get to it in due course).

Last edited 2012


matibee(Posted 2012) [#12]
To your question above (post #9)

"Method" must be defined inside the Type -> End Type code. How else would the compiler know what type the method belongs to? ;)


dynaman(Posted 2012) [#13]
method is just a different name for a function that exists in a type.

I think of them as synonyms. Another way to think of it is that a method is a function that resides in a type.


matibee(Posted 2012) [#14]
method is just a different name for a function that exists in a type.


A flawed summary on so many levels ;)


hugblue(Posted 2012) [#15]
let me see if i got it right now.

when methods is used inside types. then the methods is assigned to the different fields within the type.

and functions are used outside types. for running different operations.

or maybe not so simplistic.

and again. thz for the help. but is hard to grab when i come from procedural programming // eg : spaghetti programming.

i have a project i want to convert to oop. is so tired of the mesh it makes when theres no structure in it.

but anyway.
its oop now. until i get/understand it.


Yasha(Posted 2012) [#16]
If we take a step back from BlitzMax, what OOP is is the idea that objects respond to messages with functionality they contain. This is fundamentally different from procedural code (so it's natural to be a little confused), because in procedural code the data objects are passed to, or through, procedures that act on them in a kind of outside-in way. OOP is the opposite of this.

Perhaps you'll find it simpler if you forget altogether about functions and methods being related. It happens that they're implemented that way in BlitzMax, but conceptually they're not the same thing at all:

'Procedural:
ACTION (x) WITH (data object y)

'Object-oriented
(object y) PLEASE DO ("x")


See the difference? Procedural code structures actions together to form algorithms. Object-oriented code doesn't do this: it merely asks objects to handle things internally. (In practice, all BlitzMax code is >85% procedural, but we're being theoretical for a moment here.)

How (x) is handled in the first example is laid down by the algorithm. It might be possible to pass some modifier in as a flag or function pointer, but fundamentally it is a separate action that is applied by the code structure to the object. In the second, the algorithm doesn't incorporate (x), it merely knows that (y) is capable of handling it, and it makes the request to (y) to do so (in this case, by name, to emphasise the fact that the algorithm doesn't "have" or "own" (x)). (y) will then do it on its own terms. Importantly, this means that in the second example, the code structure doesn't know what code will be run as a result of the request: that code belongs only to (y); whereas in the first, even if it's variable, the code belongs to the outer structure.

So what a method really is is another kind of property of the object. It is distinct from a procedural function, which is a property of the code structure. You don't "call" a method like you call a function: you tell the object that you want it to either call the method or at least respond to it in some helpful way.

BlitzMax merely lets you define methods in a way that looks a lot like functions, because most of the time you'll use them in an interchangeable way. But it is important that you understand that they aren't the same thing at all.

Last edited 2012


hugblue(Posted 2012) [#17]
>> yasha ..

thz. a start is to know that they are not equally the same. else i think it will come little by little over time. is reading :: object-oriented programming in blitzmax by john judnich. and in the beginning of this book/tutorial. i saw these :

functions.
methods.

and couldnt distance myself from seeing they both called different thing. and in the same sentense looked very alike.

but thz. for the excelent help from you all. love to be in this forum. and think one day i can constribute the same great way u all do.

but for now. i will continue with the reading. and for now forget about the difference in functions and methods.

only one way. training and more training and lots of reading.


Midimaster(Posted 2012) [#18]
I try to find a not as theoretic way to understand Methods() and Functions() inside of Types for you:

A Type is a group of members of the same kind. F.e let us talk about cars. The Type is "Mercedes" and your car is one of them.

Type Mercedes
     Global MaxSpeed%, Weight%
     Field MySpeed%, MyWeight%, X%,Y%
End Type


Both, Functions() and Methods() are inside this Type, but the Functions() are related to the group, but the Methods are related to a individual car, f.e. your mercedes.

The MaxSpeed of all Mercedes is the same (150mph), but you can drive your car from 10mph to 150mph. This demonstrates the difference of Globals and Fields. The same with weight, you can load a lot of things in your car to make its weight different to the normal weight of this type of car.

Now the functions():

In Functions you do things like "creating" a new car. Or asking or changing general properties of this group:

Type Mercedes
     Function CreateNewCar:Mercedes()
          Return New Mercedes
     End Function

     Function ChangeMaxSpeed(Value%)
          MaxSpeed=Value
     End Function
End Type


to call such a Function you have to use the name of the Type:

Global Car101:Mercedes
Car101=Mercedes.CreateNewCar()


Now you have a new individual car from the type mercedes. To change things related to this specific car you have to use the Fields and you can use Methods()

Car101.MySpeed=78
Car101.SetPosition 300,200
Type Mercedes
    .....
    Method SetPosition(newX%, newY%)
          X=newX
          Y=newY
    End Method
End Type


Typical Methods are Drawing the car, moving it or call him continously for checking something like collisions.

These things (in real live) you are not able to do with the whole group, but only with individual members, because all are independent.


hugblue(Posted 2012) [#19]
>> Midimaster

thz alot. getting there for every post. smiling.
but is hard to grab. but nice text.

i will save all information and what i have learned. and when i have learned oop. i will put it up for other people in here to get. could hopefully help alot others.

is nice how helpfull u all are.

THZ.

Last edited 2012