A C
function that is main()
. We can also divide the code into different functions. Each performs a specific task.
In a function declaration, the name of the function, its return type, and parameters are specified. Function definition contains the body of the function. There is also a variety of built-in functions available in Objective-C. For example, the append string method is used to append a string to another string.
-(return type) method_name: (argument_Type1) argument_Name1joining_argument2: (argument_Type2) argument_Name2 …… {Function body}
void
.#import <Foundation/Foundation.h> @interface TestClass:NSObject /*function declaration */ - (int)sum:(int)n1 andN2:(int)num2; @end @implementation TestClass /* function returning the sum of two numbers */ - (int)sum:(int)n1 andN2:(int)n2 { /* local variable declaration */ int ans; ans=n1+n2; return ans; } @end int main () { /* local variable definition */ int x = 500; int y = 150; int r; TestClass *testClass = [[TestClass alloc]init]; /* calling a method to get the sum of two numbers */ r = [testClass sum:x andN2:y]; NSLog(@"The sum is : %d\n", r ); return 0; }