Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Objects and method parameters and return types in Objective C

    • 0
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.6k
    Comment on it

    Objects: In general objects are the real world entity. Earlier the two functions communicate with each other using the function call(say programming in c) but the programmer need more better approach for communication between the two functions or classes so the concept of object is implemented. Using the concept of objects the message passing is became more easier and programmer can use it in more effective way to pass the message from one class to another or for accessing the members of the class.

    Usage of objects:

    1) To allocate the memory.

    2) To access the member of the class(that include member Function and member functions).

    3) Reusability Of code.

    how to Create Objects:

    @interface Person : NSObject
    @property NSString *firstName;
    @property NSString *lastName;
    -(void)showEmplRecord();
    @end
    

    here we have created a class call person and it have some attributes like firstname and lastname and they are of string dataType for accessing them use the object of that class for example

    classname  *objectname=[[classname alloc]init];
    object of the class will be created as:
    Person *perobj=[[Person alloc]init];
    And we can access the class member as: 
    Perobj.firstName;
    Perobj.lastName;
    

    Methods: A method is a procedure or process to get some task done with some specific instructions. In other language like C, the method name is refer to a block of code in receiver class by compiler. In Objective C language the receiver of message is identified at run time with the help of object on which the message being passed. That means at compile time message passing system do not check if there is any receiver of message to receive it or not. It can happen that the method for the message is not implemented in receiver class. In this case it will raise an exception.
    Class Methods are called on the class name itself, independent of an instance: [classname methodName]; in above example method can be called as [Person showEmplRecord];


    Parameter of a method: 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 arguments of a method. Arguments are optional that is, a method may contain no argument. If the method has no argument is the parameter list will be blanked. And if the parameter has some argument they are listed after the method name like this-
    Method with no argument: - (void) max;
    Method with 1 argument: : - (void) max:(int) num1 ;
    Method with More Than 1 argument: - (void) max:(int) num1 secondNumber: (int) num2 ;


    Return type of a method: The return type of a function defines the type of the result that method needs to return if it was declared with a return type. The resultant return type must match the return type of method declaration. To return a value from method to message passing object return keyword must be used. If a function is declared with return type void, a return statement containing an expression generates a warning and the expression is not evaluated.  Simple code to under stand the concept of return:

    #import Foundation/Foundation.h>
    -(int)sum: (int )a secondPara:(int)b;
    int main (int argc, const char * argv[]) 
    { 
    NSLog (@"Sum Of the No are=%d");    
    return 0; 
    }
    -(int)sum: (int )a secondPara:(int)b
    {
    return a+b;
    }
    

    If a method is declared with any return type than it should return a value.. See In example the function sum declared with the int datatype so it is returning the int value(return a+b). and the main method is returning zero to the operating system that it is terminating normally.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: