[C + +] value passing and reference passing

Source: Internet
Author: User

Concept
When defining a function, the variable name in the parentheses of the function becomes the formal parameter , or the formal parameter or the virtual argument;
When a function is called in the keynote function, the parameter name in parentheses of the function is the actual argument , or argument, which can be a constant, a variable, or an expression.
Attention:

    1. Data passing between arguments and formal parameters in C is a one-way value pass.
    2. The parameters of the called function are temporarily allocated only when the function is called, and the memory that is consumed when the call ends is freed.
    3. Value passing includes value passing and pointer passing (address value), which is passed a copy of the argument.
void Exchange (int x, int y) {printf ("x:%d,y:%d\n", X, y);    printf ("&x:%x,&y:%x\n", &x, &y);    printf ("------------\ n");    int temp = x;    x = y;    y = temp;    printf ("x:%d,y:%d\n", X, y); printf ("&x:%x,&y:%x\n", &x, &y);}    void Exchange1 (int* x, int *y) {printf ("*px:%d,*py:%d\n", *x, *y);    printf ("px:%x,py:%x\n", X, y);    printf ("&px:%x,&py:%x\n", &x, &y);    printf ("------------\ n");    int temp = *x;    *x = *y;    *y = temp;    printf ("*x:%d,*y:%d\n", *x, *y);    printf ("x:%x,y:%x\n", X, y); printf ("&x:%x,&y:%x\n", &x, &y);}    void Exchange2 (int &x, int &y) {printf ("x:%d,y:%d\n", X, y);    printf ("&x:%x,&y:%x\n", &x, &y);    printf ("------------\ n");    int temp = x;    x = y;    y = temp;    printf ("x:%d,y:%d\n", X, y); printf ("&x:%x,&y:%x\n", &x, &y);}    int main () {int x, y;    x = 3;    y = 5;    printf ("x:%d,y:%d\n", X, y); printf ("&x:%x,&y:%X\n ", &x, &y);    printf ("------------\ n");    printf ("Value passed \ n");    Exchange (x, y);    printf ("Argument \ n");    printf ("x:%d,y:%d\n", X, y);    printf ("&x:%x,&y:%x\n", &x, &y);    printf ("pointer passing \ n");    int *px, *py;    PX = &x;    PY = &y;    Exchange1 (px,py);    printf ("Argument \ n");    printf ("*x:%d,*y:%d\n", *px, *py);    printf ("x:%x,y:%x\n", px, py);    printf ("&x:%x,&y:%x\n", &px, &px);    printf ("quote pass \ n");    Exchange2 (x, y);    printf ("Argument \ n");    printf ("x:%d,y:%d\n", X, y);    printf ("&x:%x,&y:%x\n", &x, &y); Cin.get ();}
Value passing

The passed argument is a copy of the argument the key function passes the parameter to the calling function, actually passing the copy of the argument (that is, the temporary copy) to the called function, not the argument itself, so that the called function cannot directly modify the value of the variable in the key function, but only the value of its private temporary copy.

x:3,y:5&x:cff89c,&y:cff890------------值传递x:3,y:5&x:cff7a0,&y:cff7a4------------x:5,y:3&x:cff7a0,&y:cff7a4实参x:3,y:5&x:cff89c,&y:cff890
x:3,y:5&x:cff89c,&y:cff890------------指针传递*px:3,*py:5px:cff89c,py:cff890&px:cff7a0,&py:cff7a4------------*x:5,*y:3x:cff89c,y:cff890&x:cff7a0,&y:cff7a4实参*x:5,*y:3x:cff89c,y:cff890&x:cff884,&y:cff884
Reference delivery

An operation on a reference is equal to the operation of its specified object, and when the argument is passed to the parameter, the parameter points to the argument (the form participates in the argument synonymous, is one of its aliases)
In the process of reference passing, the formal parameters of the called function, although the same as local variables, open up memory space in the stack, but this is the address of the argument variable that is put in by the key function. Any operation of the modulated function on the formal parameter is handled as an indirect addressing, that is, the argument variables in the central Melody function are accessed through the address stored in the stack. Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.

引用传递x:5,y:3&x:cff89c,&y:cff890------------x:3,y:5&x:cff89c,&y:cff890实参x:3,y:5&x:cff89c,&y:cff890

Reference
Value passing and reference passing-----Two ways of passing a function parameter

[C + +] value passing and reference passing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.