Objective C Tutorial - Objective C function






A function groups statements together to perform a task.

main() is a function.

A function has a function's name, return type, parameters and function body.

function sometime is also called as method.

Syntax

The general form of a function definition is as follows:

- (return_type) method_name:( argumentType1 )argumentName1 
    joiningArgument2:( argumentType2 )argumentName2 ... 
{
   body of the function
}

Here are all the parts of a method:

Return Type: The return_type defines the data type of the value the function returns. Use void as the return type if the function is not returning any value.

Method Name is the name of the function. The function name along with the parameters is called the function signature.

Arguments accepts passed-in values to the function. A function may contain no argument.

Joining Argument makes it easier to read and to make it clear while calling it.

Method Body: contains statements that will be executed when calling the function.





Example

The following code shows how to define a function called compare().

#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(@"Max value is : %d\n", ret );
 
   return 0;
}




Command-Line Arguments

We can pass some values from the command line to Objective-C programs when they are executed.

The command line arguments are handled with main() function arguments.

  • argc refers to the number of arguments passed,
  • argv[] is a pointer array, which points to each argument passed to the program.

The following is a simple example, which checks if there is any argument supplied from the command line.

#import <Foundation/Foundation.h>

int main( int argc, char *argv[] )  
{
   if( argc == 2 )
   {
      NSLog(@"The argument supplied is %s\n", argv[1]);
   }
   else if( argc > 2 )
   {
      NSLog(@"Too many arguments supplied.\n");
   }
   else
   {
      NSLog(@"One argument expected.\n");
   }
}

Creating Methods

@interface MyClass : NSObject {

}

-(void)displayMessage;

@end


@implementation MyClass

- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
        NSLog (@"Hello, world!");
    }
    
    return self;
}

-(void)displayMessage
{
    NSLog (@"Good-bye!");
}

@end

#import <Foundation/Foundation.h>
 
int main ()
{
  MyClass *testObject = [[MyClass alloc] init];
  [testObject displayMessage];
  [testObject release];
 
   return 0;
}

The code above generates the following result.

Passing By Reference

@interface MyClass : NSObject {

}

-(void)displayMessage: (NSString *) myName count:(int)myLoop;
-(int)calculateValue: (int *)outsideData;

@end


@implementation MyClass

- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
        NSLog (@"Hello, world!");
    }
    
    return self;
}

-(void)displayMessage: (NSString *) myName count:(int)myLoop;
{
    NSLog (@"Hello, %@", myName);
    NSLog (@"The loop will repeat %i times", myLoop);
    int i;
    for (i = 0; i < myLoop; i++)
    {
        NSLog (@"The value of i = %i", i);
    }
}

-(int)calculateValue: (int *)outsideData
{
    int hold;
    hold = *outsideData + *outsideData;
    *outsideData = 99;
    return hold;
}

@end

#import <Foundation/Foundation.h>
 
int main ()
{
  MyClass *testObject = [[MyClass alloc] init];
    int counter = 1;
    NSLog (@"The value of counter = %i", counter);
    int tempVar;
    tempVar = [testObject calculateValue:&counter];
    NSLog (@"Now the value of counter = %i", counter);
    [testObject displayMessage:@"Jack" count: tempVar];
    [testObject release];
 
   return 0;
}

The code above generates the following result.

Passing Objects as Parameters

@interface MyClass : NSObject {

}

-(void)displayMessage: (NSString *) myName count:(int)myLoop;

@end


@implementation MyClass

- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
        NSLog (@"Hello, world!");
    }
    
    return self;
}

-(void)displayMessage: (NSString *) myName count:(int)myLoop;
{
    NSLog (@"Hello, %@", myName);
    NSLog (@"The loop will repeat %i times", myLoop);
    int i;
    for (i = 0; i < myLoop; i++)
    {
        NSLog (@"The value of i = %i", i);
    }
}


@end

#import <Foundation/Foundation.h>
 
int main ()
{
  MyClass *testObject = [[MyClass alloc] init];
  [testObject displayMessage: @"Jack" count:5];
  [testObject release];
 
   return 0;
}

The code above generates the following result.

Passing Parameters

@interface MyClass : NSObject {

}

-(void)displayMessage: (int)myLoop;

@end


@implementation MyClass

- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
        NSLog (@"Hello, world!");
    }
    
    return self;
}

-(void)displayMessage: (int)myLoop
{
    NSLog (@"The loop will repeat %i times", myLoop);
    int i;
    for (i = 0; i < myLoop; i++)
    {
        NSLog (@"The value of i = %i", i);
    }
}

@end

#import <Foundation/Foundation.h>
 
int main ()
{
  MyClass *testObject = [[MyClass alloc] init];
  [testObject displayMessage:5];
  [testObject release];
 
   return 0;
}

The code above generates the following result.

Returning Values From a Method

@interface MyClass : NSObject {

}

-(void)displayMessage: (NSString *) myName count:(int)myLoop;
-(int)calculateValue;

@end


@implementation MyClass

- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
        NSLog (@"Hello, world!");
    }
    
    return self;
}

-(void)displayMessage: (NSString *) myName count:(int)myLoop;
{
    NSLog (@"Hello, %@", myName);
    NSLog (@"The loop will repeat %i times", myLoop);
    int i;
    for (i = 0; i < myLoop; i++)
    {
        NSLog (@"The value of i = %i", i);
    }
}

-(int)calculateValue
{
    return 4;
}


@end

#import <Foundation/Foundation.h>
 
int main ()
{

   MyClass *testObject = [[MyClass alloc] init];
  int tempVar;
    tempVar = [testObject calculateValue];
  [testObject displayMessage:@"Jack" count: tempVar];
  [testObject release];
   return 0;
}

The code above generates the following result.

Returning Values From a Method version 2


@interface MyClass : NSObject {

}

-(void)displayMessage: (NSString *) myName count:(int)myLoop;
-(int)calculateValue: (int)outsideData;

@end

@implementation MyClass

- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
        NSLog (@"Hello, world!");
    }
    
    return self;
}

-(void)displayMessage: (NSString *) myName count:(int)myLoop;
{
    NSLog (@"Hello, %@", myName);
    NSLog (@"The loop will repeat %i times", myLoop);
    int i;
    for (i = 0; i < myLoop; i++)
    {
        NSLog (@"The value of i = %i", i);
    }
}

-(int)calculateValue: (int)outsideData
{
    int hold;
    hold = outsideData + outsideData;
    return hold;
}


@end


#import <Foundation/Foundation.h>
 
int main ()
{
  MyClass *testObject = [[MyClass alloc] init];
  int tempVar;
    tempVar = [testObject calculateValue:1];  
  [testObject displayMessage:@"Jack" count: tempVar];
  [testObject release];
 
   return 0;
}

The code above generates the following result.