Parameter, real parameter, by value, by reference, by pointer

Source: Internet
Author: User

Parameter: "formal parameter" is a parameter used when defining the function name and function body. It is used to receive parameters sent when the function is called.
Real parameters: All called "actual parameters" are parameters that pass the function during the call.
 
The type of the form parameter and the real parameter must be the same, or the implicit conversion rule must be met,
When the form parameter and the real parameter are not of the pointer type
Parameters are different variables in different locations in the memory.
Copy the content of the parameter and release the parameter at the end of the function,
The real parameter content will not change.
 
If the function parameter is a pointer type variable
The real parameter address is used to pass a function.
The real parameter address, that is, the real parameter itself.
You can change the value of a real parameter.
 
Write a function that exchanges values a and B.
By value (C ++ ):
 
Function declaration: swap (x, y ){.....}
Function call: int a = 5; int B = 6;
Swap (a, B)
Note: Pass the values of a and B to x and y respectively. In fact, the value of a and B does not change. a and B are not successfully exchanged. Values of two numbers cannot be transferred by value.
Cout <a; // a = 5
Cout <a; // B = 6
 
Reference (C ++ ):
Function declaration: swap (& x, & y ){.....}
Function call: int a = 5; int B = 6;
Swap (a, B)
Note: Pass the values of a and B to x and y respectively. In fact, the value of a and B has not changed.
Cout <a; // a = 6
Cout <a; // B = 5
 
Pointer (C ++ ):
Function declaration: swap (* x, * y ){.....}
Function call: int * a = 5; int * B = 6;
Swap (a, B)
Note: Pass the values of a and B to x and y respectively. In fact, the value of a and B has not changed.
Cout <a; // a = 6
Cout <a; // B = 5

Author: ERDP Technical Architecture"

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.