Objective C functions

In this session we will have a close look at functions and how to give parameters to a function in objective C,

Before beginning with function lets have a look at what are functions and why are they used?

Functions: A function is a small set of instructions designed to operate on its given input (aka parameters or arguments) and perform some action or return some output. Generally in programming a commonly used strategy is to take a large program and break it into smaller chunks, which are turned into functions. Each function has a unique property which is given as follows:

1. Every function has a unique name. This name is used to call function from “main()” function. A function can be called from within another function.

2. A function is independent and it can perform its task without interfering with other parts of the program.

3. A function performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc.

4. A function returns a value to the calling program. This is optional and depends upon the task your function is going to accomplish. Suppose you want to just show few lines through function then it is not necessary to return a value. But if you are calculating area of rectangle and wanted to use result somewhere in program then you have to send back (return) value to the calling function.

What is a function call or calling a function?

This is the basic question that comes into a student’s mind when he or she listens these words. Well in simple terms I would say that you give a command to the OS to execute a particular block of code that you have already wrote within your program. In some higher languages like java, cpp and C#, functions are called with the help of objects same is the case with objective C, you will make the objects of a class inorder to call an object method and to call a static function (class function) you will use the class name preceded by the function name.

Lets have a look at function declaration in objective c

Syntax for class functions or static functions is given below:

+(void)displaydata;


Syntax for object function is given below:

-(void)displaydata;


The + sign is used to make a class level method (static method) and – sign is used to make an object method.

In functions we also declare parameter given below is the syntax on how to declare a function which accepts a parameter:

Syntax of function parameter

-(void)functionwithparameter1:(int)x;


since the above function is an object function then in this case you have to make object of your class and then call the above function, lets say the name of the class is Myclass then all you have to do is create an object of Myclass and then call that function. Here's an example of how would you do it

Function call:

Myclass *obj = [[Myclass alloc]init];   //cretaed an object of Myclass

[obj functionwithparameter1:7];           // called the function via object of Myclass


Now let's say you have more than one parameter in your function then in that case the syntax would be:

Syntax of function with more than one parameter

-(void)functionwithparameter2:(int)x :(int)y;


Simple right,

Now their is one more method with the help of which you can declare function with more than one parameter

-(void) Myname:(NSString*)name Livein:(NSString*)livein; 

The name of the above function is Mynamelivein which accepts two parameter of NSString object

In the above function called MynameLivein it first accepts the string value for your name and then the value for the place where you live.
of course you can declare the same function like you did for the functionwithparamete2
function

-(void) MynameLivein:(NSString*)name:(NSString*)livein; 


any of the two methods of declaring functions with two parameters will work, its upto you to choose which style of function declaration are you comfortable with


calling MynameLivein function:


[obj Myname:@"Radix" Livein:@"Pune"];  //obj is the object of the class where you have wrote this function


as you can see here Myname will first accept a name value and then value for livein





Comments