鍍金池/ 教程/ C/ Objective-C 函數(shù)按值調(diào)用
Objective-C 多態(tài)性
Objective-C 預(yù)處理器
Objective-C for循環(huán)
Objective-C 開(kāi)發(fā)環(huán)境(安裝配置)
Obj-C Foundation/基礎(chǔ)框架
Objective-C 指針運(yùn)算
Objective-C 循環(huán)
Objective-C 錯(cuò)誤處理
Objective-C while循環(huán)
Objective-C if語(yǔ)句
Objective-C do...while循環(huán)
Objective-C教程
Objective-C 嵌套switch語(yǔ)句
Objective-C 函數(shù)按值調(diào)用
Objective-C 常量
Objective-C 運(yùn)算符
Objective-C 數(shù)據(jù)類型
Objective-C 邏輯運(yùn)算符
Objective-C 數(shù)組作為函數(shù)參數(shù)傳遞
Objective-C struct/結(jié)構(gòu)
Objective-C 嵌套循環(huán)
Objective-C 函數(shù)引用調(diào)用
Objective-C 日志處理
Objective-C 數(shù)據(jù)存儲(chǔ)
Objective-C 教程首頁(yè)
Objective-C 擴(kuò)展
Objective-C 異常處理
Objective-C 類型轉(zhuǎn)換
Objective-C 嵌套 if 語(yǔ)句
Objective-C typedef
Objective-C 決策
Objective-C 指針的數(shù)組
Objective-C Protocols/協(xié)議
Objective-C 日期和時(shí)間
Objective-C 指針
Objective-C 內(nèi)存管理
命令行參數(shù)
Objective-C NSString/字符串
Objective-C 動(dòng)態(tài)綁定
Objective-C 復(fù)合對(duì)象
Objective-C Arrays/數(shù)組
Objective-C 位運(yùn)算符
Objective-C 指向指針的指針
Objective-C 從函數(shù)返回?cái)?shù)組
Objective-C Posing/冒充
Objective-C Categories/類別
Objective-C 文件處理
Objective-C 賦值運(yùn)算符
Objective-C 函數(shù)返回指針
Objective-C if...else 語(yǔ)句
Objective-C switch語(yǔ)句
Objective-C URL加載系統(tǒng)
Objective-C 算術(shù)運(yùn)算符
Objective-C Numbers/數(shù)字
Objective-C語(yǔ)言程序結(jié)構(gòu)
Objective-C 快速枚舉
Objective-C 基本語(yǔ)法
Objective-C 類&對(duì)象
Objective-C 變量
Objective-C 關(guān)系運(yùn)算符
Objective-C 塊
Objective-C break語(yǔ)句
Objective-C continue語(yǔ)句
Objective-C 語(yǔ)言概述
Objective C 文本和字符串
Objective-C 函數(shù)
Objective-C 傳遞函數(shù)的指針
Objective-C 數(shù)組的指針
Objective-C 多維數(shù)組
Objective-C 繼承
Objective-C 數(shù)據(jù)封裝

Objective-C 函數(shù)按值調(diào)用

按值傳遞參數(shù)給函數(shù)的方法調(diào)用復(fù)制到正式參數(shù)的函數(shù)參數(shù)的實(shí)際值。在這種情況下,該函數(shù)內(nèi)的參數(shù)所做的更改參數(shù)沒(méi)有影響。

默認(rèn)情況下,使用的Objective-C編程語(yǔ)言調(diào)用值法來(lái)傳遞參數(shù)。在一般情況下,這意味著,在一個(gè)函數(shù)中的代碼可以用來(lái)調(diào)用該函數(shù)的參數(shù)不會(huì)改變??紤]函數(shù)swap()定義如下:

/* function definition to swap the values */
- (void)swap:(int)num1 andNum2:(int)num2
{
   int temp;

   temp = num1; /* save the value of num1 */
   num1 = num2;    /* put num2 into num1 */
   num2 = temp; /* put temp into num2 */
  
   return;
}

現(xiàn)在,讓我們通過(guò)在下面的示例中的實(shí)際值作為調(diào)用函數(shù)swap():

#import <Foundation/Foundation.h>

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

@implementation SampleClass

- (void)swap:(int)num1 andNum2:(int)num2
{
   int temp;

   temp = num1; /* save the value of num1 */
   num1 = num2;    /* put num2 into num1 */
   num2 = temp; /* put temp into num2 */
   
}

@end

int main ()
{
   /* local variable definition */
   int a = 100;
   int b = 200;
   
   SampleClass *sampleClass = [[SampleClass alloc]init];

   NSLog(@"Before swap, value of a : %d
", a );
   NSLog(@"Before swap, value of b : %d
", b );
 
   /* calling a function to swap the values */
   [sampleClass swap:a andNum2:b];
 
   NSLog(@"After swap, value of a : %d
", a );
   NSLog(@"After swap, value of b : %d
", b );
 
   return 0;
}

讓我們編譯并執(zhí)行它,它會(huì)產(chǎn)生以下結(jié)果:

2013-09-09 12:12:42.011 demo[13488] Before swap, value of a : 100
2013-09-09 12:12:42.011 demo[13488] Before swap, value of b : 200
2013-09-09 12:12:42.011 demo[13488] After swap, value of a : 100
2013-09-09 12:12:42.011 demo[13488] After swap, value of b : 200

這表明,盡管它們均已改變,在函數(shù)內(nèi)部的值中沒(méi)有任何改變。