Is there any dramatic difference between functions and methods in Objective -C?
Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This eliminates the need for writing the same code again and again.
A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not.
Difference Between Function and Method:A function can pass the data that is operated and may return the data. The method operates the data contained in a Class. Data passed to a function is explicit. A method implicitly passes the object on which it was called.
Unlike a function, methods are called on an object. Like in our example above we call our method . i.e. “my_method” on the object “cat” whereas the function “sum” is called without any object. Also, because the method is called on an object, it can access that data within it.
First, I'm a beginner in Objective-C, but I can say what I know.
Functions are code blocks that are unrelated to an object / class, just inherited from c, and you call them in the way:
// declaration int fooFunction() { return 0; } // call int a; a = fooFunction();
While methods are attached to class / instance (object) and you have to tell the class / object to perform them:
// declaration - (int)fooMethod { return 0; } // call int a; a = [someObjectOfThisClass fooMethod];
It is even simpler; a method is just a C function with the first two argument being the target of the method call and the selector being called, respectively.
I.e. every single method call site can be re-written as an equivalent C function call with absolutely no difference in behavior.
In depth answer here: Why [object doSomething] and not [*object doSomething]? Start with the paragraph that says "Getting back to the C preprocessor roots of the language, you can translate every method call to an equivalent line of C".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With