codescracker


objective c

Objective-C Functions



« Previous Tutorial Next Tutorial »


A function is a block/group of statement(s) that together perform a particular task. Each and every Objective-C program consists of at least one function which is main(), since all the program execution begins from the function main().

Function plays an important role in bigger project. Since you can divide your complex and bigger code into separate functions. It is up to you, that how you will divide the programs into separate functions, so that each functions perform a particular task.

Declare a Function in Objective-C

A function declaration simply tells the compiler about the function's name, function's return type, and then the function's parameters. A function declaration tells the compiler about how to call the function. The actual body of the function can also be defined separately. Here is the general form to declare a function in Objective-C:

- (return_type) function_name:(argumentType1)argumentName1 
joiningArgument2:(argumentType2)argumentName2 ... 
joiningArgumentn:(argumentTypeN)argumentNameN;

For the above-defined function named max(), following is the function declaration:

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

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

Define a Function in Objective-C

A function definition in Objective-C provides the actual body of the function. Here is the general form to define a function in Objective-C:

- (return_type) function_name:(argumentType1) argumentName1 
joiningArgument2:(argumentType2)argumentName2 ... 
joiningArgumentn:(argumentTypeN)argumentNameN
{
   body of the function
}

In Objective-C, a function definition consists of a function header and a function body. Following are the description of all the parts of the above function declaration code fragment:

Let's look at the following source code for the function called max(). This function takes two parameters num1 and num2 and returns the maximum between the two numbers:

/* Objective-C Functions - Example Program
 * This program returns the maximum number
 * between the two numbers */
		
- (int) max:(int) num1 secondNumber:(int) num2 
{
	int res;
	if(num1>num2)
	{
		res = num1;
	}	
	else
	{
		res = num2;
	}	
	return res; 
}

Call a Function in Objective-C

After declaring and defining a function, you need to call that function to use the function in the program. When an Objective-C program calls a function, then the program control goes to the called function. A called function performs the 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, from where the function is called.

To call a function in Objective-C, you need to pass the required parameters along with the function name, and if function returns a value, then you can store returned value in a variable, or you can also print the returned value directly to the output screen.

Objective-C Function Example

Here is an example illustrating the concept of function declaration, function definition, and then the function calling practically

/* Objective-C Functions - Example Program */
		
#import <Foundation/Foundation.h>
@interface SampleClass:NSObject
/* function declaration */
- (int)max:(int)num1 andNum2:(int)num2;
@end

@implementation SampleClass

/* function returning the max between two numbers */
- (int)max:(int)num1 andNum2:(int)num2
{
	int res;
	if(num1>num2)
	{
		res = num1;
	}	
	else
	{
		res = num2;
	}
	return res; 
}
@end

int main()
{
	int a = 10;
	int b = 20;
	int ret;
	SampleClass *sampleClass = [[SampleClass alloc]init];
	/* calling a method to get the maximum value */
	ret = [sampleClass max:a andNum2:b];
	NSLog(@"Maximum Value = %d\n", ret);
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-03 13:18:37.932 demo[26080] Maximum Value = 20

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test