promotion image of download ymail app
Promoted

Call by value,Call by ref是什麼

什麼是Call by value 和 Call by reference??他們分別的內容??差異性??

拜託各位大大解決小弟問題!!

2 Answers

Rating
  • 昱仁
    Lv 5
    1 decade ago
    Favorite Answer

    有三種喔,分別是 call by value, call by address, call by reference.

    那其中call by address和call by reference是比較像的, 但是還是有些許不同.

    1. Call by value

    就是傳值呼叫,

    它會再你傳入參數的時候建立一個獨立的物件,

    來儲存你傳入的參數,

    也就是說你傳入的值並不會改變到你原本記憶體的參數,

    它只會改變臨時建構的物件,

    這個物件會在函式呼叫完的時候解構~.

    2. call by address

    就是傳址呼叫,

    它並不會另外建立一個物件,

    它會把指向位置的指標傳入,

    所有的陣列傳遞都是用指標達成的,

    而且你的指標可以只向別的位置,

    也就是說你用傳址呼叫,

    你改變的參數就是你原本記憶體的參數!

    3.call by refence

    就是傳參考呼叫,

    它也是不會另外建立物件,

    它會建立一個指向呼叫端物件的參考,

    所以跟call by address一樣,

    你改變它的值,它就會改變你原本記憶體的值,

    然後和傳址呼叫不同的是,

    參考是常數,指標是變數.

    其他更深入的講解,

    你可以參考龍大的~

    http://tw.knowledge.yahoo.com/question/question?qi...

    call by value 和 call by reference 很好分辨,

    就是會不會改到原本記憶體的變數內容囉!!!

    Source(s): 知識+(龍大) & 經驗
    • Commenter avatarLogin to reply the answers
  • 1 decade ago

    Wait, there are many more other parameter passing strategies:

    Strict

    evaluation: includes

    Call

    by value

    Call

    by reference/address

    Call

    by sharing

    Call

    by copy-restore

    Partial

    evaluation: includes

    Non-strict

    evaluation

    Call

    by name

    Call

    by need

    Call

    by macro expansion

    Nondeterministic

    strategies

    Call

    by future

    Optimistic

    evaluation

    For ordinary programmers, you probably can live with the understanding of

    call by value, by address, by reference, and by name.

    But for computer scientists, you need to know each and everyone of them, and of course, why they are used by some programming languages but not the others. To do this, I think you need to know one thing first - "Side Effect".

    When you design and subroutine, in addition to the functions the subroutine need to perform, you also need to determine how the subroutine should communicate with the caller. In mathematical normal form, the direction of information flows and the mechanism is very well defined, and more importantly, well understood; however, in computer programming, we may not need to be so strict. Thus, so many mechanisms are proposed to allow flexibility. So,

    Call by Value - advocated by C, is to restrict the information flow to one direction only (from caller to callee), however, due to the availability of pointer type, the side effect allows the information flows in the other direction (from callee to caller.)

    Call by reference - reference is same as address (pointer) without the need for explicit de-reference. FORTRAN has it for years but C disallow it. However, C++ and C# bring it back for certain reason.

    2009-11-29 07:02:03 補充:

    However, many papers still considered it dangerous, not for technical reason, but personal reasons - for few OO programmers read class declarations carefully enough to appreciate why a formal argument is declared to be a reference type.

    2009-11-29 07:02:28 補充:

    The example my ninth grade programming teacher liked to use:

    cin and cout are soooooo commonly used by almost all OO programmers. But few care to read iostream classes to answer the following question:

    what does

    cin >> a

    evaluate to? and why it is designed that way.

    2009-11-29 07:04:34 補充:

    Too much already - need to stop now. Anyway, if you know how to answer the above questions, you probably can say with confidence that you know "reference" pretty well.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.