Function Argument - objective C
Basically two type of function argument.
If a function is to use argument, it must declare variable that accept the values of the arguments. These variables are called the formal parameters of the function.
The formal parameters behave like other local variable inside the function and are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways that argument can be passed to function.
Call by Value
The call by value method of passing argument to a function copies the actual value of an argument into the formal parameter of the function. In the case, changes made to the parameter inside the function have no effect on the argument.
By default,Objective C programming language uses call by value method to pass arguments.In general, this means that code within a function cannot alter the arguments used to call the function. Consider the function swap() definition as follows:
Example
@interface SampleClass : NSObject
- (void)swap : (int)num1 and Num2 :(int)num2;
@end
@implementation SampleClass
- (void)swap: (int)num1 andNum2 :(int)num2
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
@end
int main()
{
int a = 100;
int b = 200;
SampleClass *sampleClass = [[SampleClass alloc]init];
NSLog(@"Before swap,value of a :%d"\n" , a );
NSLog(@"Before swap,value of b :%d"\n" , b );
[sampleClass swap:a andNum2:b];
NSLog(@"After swap,value of a :%d"\n" , a );
NSLog(@"After swap,value of b :%d"\n" , b );
return 0;
}
Call by reference
The call by reference method of passing argument to a function 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 passed argument.
To pass the value by reference, argument pointers are passed to the functions just like any other value. So accordingly, you need to declare the function parameters as pointer type as in the following function swap(), which exchange the values of the two integer variables pointed to by its arguments.
To check more details about Objective C pointers, you can check Objective C pointer.
For now, let us call the function swap() by passing values by reference as in the following examle:
Example
@interface SampleClass : NSObject
- (void)swap : (int *)num1 and Num2 :(int *)num2;
@end
@implementation SampleClass
- (void)swap: (int *)num1 andNum2 :(int *)num2
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
@end
int main()
{
int a = 100;
int b = 200;
SampleClass *sampleClass = [[SampleClass alloc]init];
NSLog(@"Before swap,value of a :%d"\n" , a );
NSLog(@"Before swap,value of b :%d"\n" , b );
[sampleClass swap:a andNum2:b];
NSLog(@"After swap,value of a :%d"\n" , a );
NSLog(@"After swap,value of b :%d"\n" , b );
return 0;
}
Which show that the change has reflected outside of the function as well unlike call by value where changes does not reflect of the function.
By default,Objective C uses call by value to pass arguments. In general, this means that code within a function cannot alter the argument used to call the function, and above mentioned example while calling max() function used the same method.
If a function is to use argument, it must declare variable that accept the values of the arguments. These variables are called the formal parameters of the function.
The formal parameters behave like other local variable inside the function and are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways that argument can be passed to function.
Call Type | Description |
---|---|
Call by value | This 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 Reference | This 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. |
Call by Value
The call by value method of passing argument to a function copies the actual value of an argument into the formal parameter of the function. In the case, changes made to the parameter inside the function have no effect on the argument.
By default,Objective C programming language uses call by value method to pass arguments.In general, this means that code within a function cannot alter the arguments used to call the function. Consider the function swap() definition as follows:
- (void)swap : (int)num1 andNum1 :(int)num2
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
return;
}
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
return;
}
#import <Foundation/Foundation.h>
@interface SampleClass : NSObject
- (void)swap : (int)num1 and Num2 :(int)num2;
@end
@implementation SampleClass
- (void)swap: (int)num1 andNum2 :(int)num2
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
@end
int main()
{
int a = 100;
int b = 200;
SampleClass *sampleClass = [[SampleClass alloc]init];
NSLog(@"Before swap,value of a :%d"\n" , a );
NSLog(@"Before swap,value of b :%d"\n" , b );
[sampleClass swap:a andNum2:b];
NSLog(@"After swap,value of a :%d"\n" , a );
NSLog(@"After swap,value of b :%d"\n" , b );
return 0;
}
Call by reference
The call by reference method of passing argument to a function 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 passed argument.
To pass the value by reference, argument pointers are passed to the functions just like any other value. So accordingly, you need to declare the function parameters as pointer type as in the following function swap(), which exchange the values of the two integer variables pointed to by its arguments.
- (void)swap : (int *)num1 andNum1 :(int *)num2
{
int temp;
temp = *num1;
*num1 = *num2;
8num2 = temp;
return;
}
{
int temp;
temp = *num1;
*num1 = *num2;
8num2 = temp;
return;
}
For now, let us call the function swap() by passing values by reference as in the following examle:
Example
#import <Foundation/Foundation.h>
@interface SampleClass : NSObject
- (void)swap : (int *)num1 and Num2 :(int *)num2;
@end
@implementation SampleClass
- (void)swap: (int *)num1 andNum2 :(int *)num2
{
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
@end
int main()
{
int a = 100;
int b = 200;
SampleClass *sampleClass = [[SampleClass alloc]init];
NSLog(@"Before swap,value of a :%d"\n" , a );
NSLog(@"Before swap,value of b :%d"\n" , b );
[sampleClass swap:a andNum2:b];
NSLog(@"After swap,value of a :%d"\n" , a );
NSLog(@"After swap,value of b :%d"\n" , b );
return 0;
}
Which show that the change has reflected outside of the function as well unlike call by value where changes does not reflect of the function.
By default,Objective C uses call by value to pass arguments. In general, this means that code within a function cannot alter the argument used to call the function, and above mentioned example while calling max() function used the same method.
No comments: