Differentiating Between Functions and Methods
In Objective-C, there is a distinction between functions and methods. Functions are code blocks that are unrelated to an object or a class, just inherited from C. Methods are code blocks that are attached to a class or an instance (object) and are invoked by sending messages to them.
The syntax for defining and calling functions and methods is different. Functions use the standard C syntax, as shown in the previous sections. Methods use a special Objective-C syntax, as shown in the following example:
// Method declaration in interface section
– (return_type)method_name:(parameter_type)parameter_name;
// Method definition in implementation section
– (return_type)method_name:(parameter_type)parameter_name
{
// body of the method
}
// Method call by sending a message to an object or a class
[receiver method_name:argument];
Explanation:
- The – sign before the method name indicates that it is an instance method, which means that it can be called by an instance of a class.
- The + sign indicates that it is a class method, which means that it can be called by a class name.
- The method name consists of one or more parts separated by colons.
- Each part corresponds to a parameter of the method.
- The parameter type and name are enclosed in parentheses after each part.
- The method name and the parameter list together constitute the method signature.
- The method call is made by sending a message to a receiver, which can be either an object or a class.
- The message consists of the method name followed by the arguments enclosed in brackets.
- The arguments must match the number and type of the parameters declared by the method.
For example, the following code defines and calls a method called add that takes two integers as parameters and returns their sum:
ObjectiveC
// Auther: Nikunj Sonigara #import <Foundation/Foundation.h> // Interface section of Calculator class @interface Calculator : NSObject - ( int )add:( int )x to:( int )y; @end // Implementation section of Calculator class @implementation Calculator - ( int )add:( int )x to:( int )y { return x + y; } @end int main( int argc, const char * argv[]) { // Method call by sending a message to an object of Calculator class Calculator *calc = [[Calculator alloc] init]; int result = [calc add:10 to:20]; NSLog ( @"Result: %d" , result); // result will be 30 return 0; } |
Output:
Note that functions and methods can have the same name as long as they have different signatures. For example, we can have both a function and a method called max, but they must have different parameter lists.
Functions in Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and Cocoa Touch.
One of the features of Objective-C is that it supports functions, which are named blocks of code that can be called upon to perform a specific task. Functions can be provided with data on which to perform the task and can return a result to the code that called them. Functions can help to organize the code, avoid repetition, and improve readability and maintainability.
Contact Us