Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective c difference between functions and methods

Tags:

objective-c

Is there any dramatic difference between functions and methods in Objective -C?

like image 311
NCFUSN Avatar asked Jul 12 '11 23:07

NCFUSN


People also ask

What is the difference between function and method in 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.

Is there a difference between functions and methods?

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.

What are the three main differences between a method and a function?

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.

What is the difference between functions and methods give an example of each?

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.


2 Answers

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]; 
like image 143
MByD Avatar answered Sep 18 '22 08:09

MByD


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".

like image 45
bbum Avatar answered Sep 21 '22 08:09

bbum