All I know about the difference between them is in the image below.
scrypto101 explanantion of function and method
I am unable to pinpoint the difference between two of them with complete clarity.
Especially, I am unable to see scrypto code and point out that which one is a function
and which one is a method
.
I read that a method causes change in state where as a function does not.
What is a state
?
What does it change in state
exactly mean ?
e.g. If I have to just make some changes to a nfs's metadata etc, should that be categorised as a function or a method ? (I think method)
A guideline about distinguishing a code tasks as a function or a method will help me a lot.
I am a beginner in DeFi and blockchain technology, any explanation of above with an real life example for identifying change vs no change in state
or code snippet will help me a lot.
You can think of functions and methods just like in Object Oriented Programming where you have Classes and Objects. Classes offer functions and the instantiated objects offer methods. Since functions are called on the class itself, it doesn't have any state to view/update. Methods are called on individual objects instantiated from a class and it has access to the values stored on the state of the object.
Concretely, in Scrypto, the difference is in the first argument of the function/method. If you put &self
as the first argument, it will be a method rather than a function since you have access to the variables of the component through this &self
argument.