method and function
BlitzMax Forums/BlitzMax Beginners Area/method and function
| ||
what the difference between method and function ? |
| ||
Here are some discussions: http://www.blitzmax.com/Community/posts.php?topic=87659 or http://www.blitzmax.com/Community/posts.php?topic=89251 |
| ||
It would be a good idea for you to read about OOP (Object Orientated Programming). But here is a link that should give you some idea http://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function |
| ||
Hi yossi, I do not know your skills, so it is difficult to know how deep the explanation should go... You know what a function is and you use it often! And perhaps you also know what an object is? BadMan:Enemy = New Enemy BadMan.Beat 30 Print BadMan.Power Type Enemy Field Power%=100 Method Beat(Force%) Power=Power-Force End Method End Type This sample creates an enemy "BadMan". This enemy has the property "Power". "Power" is a FIELD, what means, that this value is individual for each enemy. Methods are used to change this individual properties. In the sample the "Method Beat" reduces the power. Afterward the power of BadMan is 70. In the next sample you can see, how a method affect only the individual property: BadMan:Enemy = New Enemy Horror:Enemy= New Enemy BadMan.Beat 30 Horror.Beat 50 Print BadMan.Power Print Horror.Power Type Enemy Field Power%=100 Method Beat(Force%) Power=Power-Force End Method End Type Functions are used to do things related to all enemies. Methods are used to do things related to one individuum. So functions are called together with the type: Enemy.PaintAll() but methods are called together with a certain individuum: BadMan.Beat 30 |
| ||
thank you very much. it was very helpful. |
| ||
In addition to the post above: use parenthesis when calling functions/methods. see: BadMan.Beat 30 Next time you want the 30 to be a value returned by a function BadMan.Beat GetDamage Next time you want GetDamage to be calculated using a param BadMan.Beat GetDamage rand 20,50 Wow.. glad this should not compile at all. Use "( )"wherever possible. BadMan.Beat( GetDamage( rand(20,50) ) ) It is also way more futureproof concerning other languages and parser changes (that brackets are one of the reasons the ".."-concat operator has to exist). @Methods and functions: Methods are functions with implicit passed objects of that class. For you of importance: methods can access individual object properties (the fields) while functions can't. Methods can only be called if an instance of that class ("type") is created and used for calling the Method. bye Ron |