C Language Learning Notes (003)-arguments and formal parameters in C + + (GO)

Source: Internet
Author: User

Today I suddenly see a question about formal parameters and arguments, I actually superficial understanding. Despise the past in my mind only one parameter of the concept, the difference between formal parameters and arguments really do not know, as a study for a few years C + + people, really deeply feel sorry for the C + + teacher T. T

I think as long as you understand the value of the delivery and address delivery, you should be able to understand the formal parameters and actual parameters of the details of the work.

1. Value passing

An argument is a variable, and an expression is equivalent.

Find (int x) {}

y= find (z);

In the above example, Z is an argument and x is a formal parameter. X is changed to Z.

During value passing, the arguments and parameters are located in two different addresses in memory, and the arguments are copied once and copied to the parameters first. Therefore, the changes in the parameters do not have any effect on the arguments during value passing.

2. Address delivery (also referred to as reference passing)

The argument is a pointer.

In the case of a function call, the argument is passed to you as a pointer address, which means that the argument is the same as the formal parameter, and the argument changes when your parameter changes.

Find (int &x) {}

y= find (z);

In the above example, Z is an argument and x is a formal parameter. Z changes with X.

3. Const reference Delivery

Find (const int &x) {}

y= find (z);

In the above example, Z is an argument and x is a formal parameter. Z does not change with X.

Some people will ask, you do this is not the same as the value of the pass? No!

Careful observation will find that in the value of the transfer of two copies, waste of memory resources is quite shameful, the appearance of const effectively avoid the occurrence of this situation, just copy once is enough.

C Language Learning Notes (003)-arguments and formal parameters in C + + (GO)

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.