Pass by value parameter passing
suggest changeIn pass by value, the argument’s value is copied into the parameter. Changing the parameter does not change the caller’s variable.
@interface SwapClass : NSObject
- (void)num:(NSInteger)num1 andNum2:(NSInteger)num2;
@end
@implementation SwapClass
-(void) num:(NSInteger)num1 andNum2:(NSInteger)num2{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
@end
Calling the methods:
NSInteger a = 10, b =20;
SwapClass *swap = [[SwapClass alloc]init];
NSLog(@"Before calling swap: a=%ld, b=%ld", (long)a, (long)b);
[swap num:a andNum2:b];
NSLog(@"After calling swap: a=%ld, b=%ld", (long)a, (long)b);
2016-07-30 23:55:41.870 Test[5214:81162] Before calling swap: a=10,b=20
2016-07-30 23:55:41.871 Test[5214:81162] After calling swap: a=10,b=20
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents