Objective-C Functions

How to use Functions in Objective-C

A capacity is a gathering of articulations that together perform an errand. Each Objective-C project has one C capacity, which is primary(), and the greater part of the most unimportant projects can characterize extra capacities.

You can separation up your code into independent capacities. How you separation up your code among distinctive capacities is dependent upon you, yet intelligently the division for the most part is so every capacity performs a particular assignment.

A capacity statement enlightens the compiler regarding a capacity's name, return sort, and parameters. A capacity definition gives the real group of the capacity.

Essentially in Objective-C, we call the capacity as technique.

The Objective-C establishment structure gives various inherent strategies that your system can call. Case in point, strategy appendString() to add string to another string.


A system is known with different names like a capacity or a sub-routine or a strategy, and so on.

Characterizing a Method

The general type of a strategy definition in Objective-C programming dialect is as per the following:

- (return_type) method_name:( argumentType1 )argumentName1 
joiningArgument2:( argumentType2 )argumentName2 ... 
joiningArgumentn:( argumentTypen )argumentNamen 
{
   body of the function
}
  • Return Type: A method may return a value. The return_type is the data type of the value the function returns. Some methods perform the desired operations without returning a value. In this case, the return_type is the keyword void.
  • Method Name: This is the actual name 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 arguments of a method. Arguments are optional; that is, a method may 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:


Taking after is the source code for a system called max(). This strategy takes two parameters num1 and num2 and gives back the greatest between the two:

/* function returning the max between two numbers */
- (int) max:(int) num1 secondNumber:(int) num2
{
/* local variable declaration */
int result;

if (num1 > num2)
{
result = num1;
}
else
{
result = num2;
}
return result;
}


Method Declarations:

Taking after is the source code for a strategy called max(). This system takes two parameters num1 and num2 and gives back the greatest between the two:

A technique assertion has the accompanying parts:

 (return_type) function_name:( argumentType1 )argumentName1 
joiningArgument2:( argumentType2 )argumentName2 ... 
joiningArgumentn:( argumentTypen )argumentNamen;

For the above-characterized capacity max(), taking after is the strategy revelation:

-(int) max:(int)num1 andNum2:(int)num2;
Strategy affirmation is obliged when you characterize a system in one source document and you call that technique in another record. In such case you ought to pronounce the capacity at the highest point of the document calling the capacity.

Calling a method:


While making an Objective-C technique, you give a meaning of what the capacity needs to do. To utilize a strategy, you will need to call that capacity to perform the characterized errand. 

At the point when a project calls a capacity, system control is exchanged to the called strategy. A called technique performs characterized assignment, and when its arrival proclamation is executed or when its capacity closure shutting support is come to, it returns project control back to the primary system. 

To call a technique, you basically need to pass the obliged parameters alongside strategy name, and if system gives back a worth, then you can store returned quality. Case in point:

#import <Foundation/Foundation.h>;

@interface SampleClass:NSObject
/* method declaration */
- (int)max:(int)num1 andNum2:(int)num2;
@end

@implementation SampleClass

/* method returning the max between two numbers */
- (int)max:(int)num1 andNum2:(int)num2{
/* local variable declaration */
int result;

if (num1 > num2)
{
result = num1;
}
else
{
result = num2;
}
}
@end


int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;

SampleClass *sampleClass = [[SampleClass alloc]init];

/* calling a method to get max value */
ret = [sampleClass max:a andNum2:b];
NSLog(@"Max value is : %d\n", ret );
return 0;
}

I kept max() work alongside principle() work and consented the source code. While running last executable, it would deliver the accompanying result:

2013-09-07 22:28:45.912 demo[26080] Max value is : 200

Function Arguments:


On the off chance that a capacity is to utilize contentions, it must pronounce variables that acknowledge the estimations of the contentions. These variables are known as the formal parameters of the capacity. 

The formal parameters act like other neighborhood variables inside the capacity and are made upon passage into the capacity and obliterated upon way out. 

While calling a capacity, there are two ways that contentions can be gone to a capacity:

Call TypeDescription
Call by valueThis method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
Call by referenceThis method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

Of course, Objective-C uses call by quality to pass contentions. When all is said in done, this implies that code inside a capacity can't change the contentions used to call the capacity, or more specified case while calling max() capacity utilized the same system.

0 comments:

Post a Comment

My Instagram