Header Ads

Functions - Ojective C

A function is a group of statements that together perform a task. Every Objective C program has one C function, which is main(), and all of the most trivial programs can define additional functions.

You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function perform a specific task.

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

Basically, in Objective C, we call the function as method.

The Objective C foundation framework provides numerous built-in methods that your program can call. For example, method appendString() to append string to another string.

A method is known with various names like a function or a sub-routine or a procedures, etc.

Defining a Method
 The general form of a method definition in Objective C programming language is a s follows:

 - (return_type) method_name:(argumenType1 )argumentName1
 joiningArgument2:(argumentType2)argumentName2...
 joiningArgumentN:(argumentTypeN)argumentNameN
 {
         statements;
 }

A method definition in objective C programming language consist of a method header and a method body. Here are all the parts of a method.
  • Return Type: A method may return value. The return_type is the data type of the value the function returns. Some methods perform the desired operation without returning a value. In this case, the return_type is the keyword void.
  • Method Name: This is the actual of the method. The method name and the parameter list together constitute the method signature.
  • Arguments: A argument is like a placeholder. When a function is invoked, you pass a value to the argument. This value is referred to as actual parameter or argument. The parameter list refers to the type, order and number of the argument of a method. Argument are optional that is, a method contain no argument.
  • Joining Argument: A joining argument is to make it easier to read and to make it clear while calling it.
  • Method Body: The method body contains a collection of statements that define what the method does.
 Example:

Following is the source code for a method called max(). This method takes two argument parameter num1 and num2 and return the maximum between the two.

 - (int) max : (int) num1 secondNumber :(int) num2
{
     int result;
     if(num1 > num2)
     {
            result = num1;
     }
     else
     {
           result = num2 ;
     }
     return result;
}

Method Declarations:

A method declaration tells the compiler about a function name and how to call the method. The actual body of the function can be defined separately.
A method declaration has the following parts:

- (return_type) function_Name :( argumentType1)argumentName1
joiningArgument2: ( argumentType2)argumentName2
joiningArgumentN:( argumentTypeN)argumentNameN;

For the above defined function max() following is the method declaration :

">- (int) max: (int)num1 andNum2 : (int)num2;

Method declaration is required when you define a method in one source file and you call that method in another file. In such case you should declare the function at the top of the file calling the function. 

 Calling a method:
When creating a objective C method, you give a definition of what the function has to do. To use a method, you will have to call that function to perform the defined task.

When a program calls a function, program control is transferred to the called method. A called method performs defined task, and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.


To call a method, you simply need to pass the required parameters along with method name, and if method returns a value, then you can store returned value. For example:

#import<Foundation/Foundation.h>
 @interface SampleClass : NSObject

     - (int) max : (int)num1 andNum2 : (int) num2;
@end

@implementation SampleClass
        - (int)max : (int)num1 andNum2 :(int)num2
          {
                int result;
                if( num1 > num2)
                 {
                          result = num1;
                 }
                 else
                 {
                         result = num2;
                 }
              return result;
         }
@end

int main()
{
         int a = 100;
         int b = 200;
         int ret;
         SampleClass *sampleClass = [[SampleClass alloc]init];
         
         ret = [sampleClass max:a andNum2 :b];
         NSLog(@"Maximum value is :%d\n", ret);
         return 0;
}

Next two point in next post.....continue 
  

No comments:

Powered by Blogger.