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

Source: Internet
Author: User


  When defining a function, the variable name in the parentheses of the function becomes a formal parameter , or a formal argument or a virtual parameter; When a function is called in the key 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.


1. The amount of data transfer between the actual parameter and the formal parameter in C language is one-way "value pass", one-way pass, can only be passed to the formal parameter by the argument, and vice versa.

    2. The parameters of the called function will only be allocated temporarily when the function is called, and the memory occupied by the end of the call will be freed.

3,"pass by value" includes the value of the pass ( real value Ah!) ) and pointer passing (the pointer pass parameter is essentially the way the value is passed, it passes an address value ) and is passed a copy of the argument.

one, by value delivery

The key function passes a parameter to the calling function, which actually simply passes 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.

  1, Example 1: Value transfer (real value AH)

1#include <stdio.h>2 intMain ()3 {  4     intA=2, b=3; 5     voidSwapintXinty); 6printf"before:%d,%d\n", A, b); 7 Swap (A, b); 8printf"later:%d,%d\n", A, b); 9     return 0; Ten }   One    A voidSwapintXinty) - {   -     inttmp;  thetmp=x;  -x=y;  -y=tmp;  -}

  Operation result :



  The following example shows whether a value or a pointer is passed (a pointer is a value ), and a copy of the argument is passed to the called function, and the copy is directly manipulated without affecting the argument

1#include <stdio.h>2 intMain ()3 {4     intA =2, B =3;5printf"before:%d,%d\n", A, b);6printf"---------------------------\ n");7printf"A is located at the address:%x\n", &a);8printf"B is located at the address:%x\n", &b);9printf"---------------------------\ n");Ten     voidSwapint* x,int*y); OneSwap (&a, &b); Aprintf"later:%d,%d\n", A, b); -     return 0; -  the } -  - voidSwapint* x,int*y) - { +printf"X is located at the address:%x\n", &x); -printf"y is located at the address:%x\n", &y); +printf"---------------------------\ n"); Aprintf"the value stored by X is:%x\n", x); atprintf"the values for y storage are:%x\n", y); -printf"---------------------------\ n"); -     int*tmp =NULL; -TMP =x; -x =y; -y =tmp; in     //so swapping x, y just swapped the value stored in both (the address of A, b), and there is no interchange involving the value of A/b -     //so just the operating address and no eggs . toprintf"the value stored by X is:%x\n", x); +printf"the values for y storage are:%x\n", y); -printf"---------------------------\ n"); the}

  Operation result :


  However, the value of the argument can be changed indirectly by address ( See example 2)

  2. Example 2: pointer passing

#include <stdio.h>intMain () {intA =2, B =3; voidSwapint* x,int*y); printf ("before:%d,%d\n", A, b); Swap (&a, &b); printf ("later:%d,%d\n", A, b); return 0;}voidSwapint* x,int*y) {    inttmp; //The use of ' * ' here means the contents of the variable that the pointer x, Y points to (that is, the contents of the variable A/b) .TMP = *x; *x = *y; *y =tmp;}

  Operation result :



Ii. passing by reference

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 )

1#include <stdio.h>2 voidSwapint& A,int&b)3 {4     inttmp;5TMP =A;6A =b;7b =tmp;8 }9 Ten intMain () One { A     inti =3, j =4; -printf"before swap:i=%d,j=%d\n", I, j); - Swap (I, j); theprintf"After swap:i=%d,j=%d\n", I, j); -     return 0; -}

  Operation result :


Third, supplementary

  As for the pointer/reference pass format, you can refer to the following content :

1 int x=1; 2 3 int // for pointer passing, Y has its own independent memory address, the stored content is the address of X, *y is the value of x 4 5 int // for reference passing, it can be understood that Z is x,x or z, except that the name is different.

  Finally, one more verbose example :

1#include <iostream>2 using namespacestd;3 intChange1 (Char*name) {4Name ="Alter";5     return 1;6 }7 8 intChange2 (Char* &name) {9Name ="Alter";Ten     return 1; One } A  - intMain () { -     Char*string="original!"; theChange1 (string); -cout <<string<<'\ n'; -  -Change2 (string); +cout <<string<<'\ n'; -  +}

  Operation result :


  Change1 is a value pass, the parameter name has its own independent memory address, and the modified name becomes the address of "alter" ( but the contents of the string or "original!" )。

  Change2 is a reference pass, the address of the parameter name is the address of a string, or name is a string.


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

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: 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.