close

Call-by-value

int a= 5;

int b = a;

直接把 a 的值複製 b 的記憶體中,因此 a 與 b 是各自有一個記憶體,大型程式中若使用太多不需要的call-by-value,可能導致記憶體不足。

Call-by-address / Call-by-pointer

使用function時,參數為使用&傳入記憶體,若是為swap(&a,&b),則代表 a 與 b 交換記憶體。

Call-by-reference

作用與call-by-address,但不用寫&,為c++為簡化call-by-address發明的方法,c無法使用。

 

比較

p.s. parameter參數 v.s. argument引數

argument 用來呼叫函式的實際值

parameter 方法的宣告

例子:

void function(int num);

int main(){

        .......

        function(a);

}

num為parameter

a為argument

 

call-by-reference parameter  比 call-by-value parameter 有效率,因為call-by-value是一個被argument initialize 的 local variable,所以argument 需要 copy 兩次,但是 call-by-reference 中的 parameter 以 placeholder 取代 argument,所以 argument 只複製一次。

 

 

 

 

 

 

arrow
arrow
    文章標籤
    c++
    全站熱搜
    創作者介紹
    創作者 KR 的頭像
    KR

    漫漫長路

    KR 發表在 痞客邦 留言(0) 人氣()