C language function call Methods: value transfer, Reference call, address transfer (question), and function

Source: Internet
Author: User

C language function call Methods: value transfer, Reference call, address transfer (question), and function

I. Questions:

Question 1: The program code is as follows:

Void Exchg1 (int x, int y)

{

Int tmp;

Tmp = x;

X = y;

Y = tmp;

Printf ("x = % d, y = % d/n", x, y)

}

Void main ()

{

Int a = 4, B = 6;

Exchg1 (a, B );

Printf ("a = % d, B = % d/n", a, B)

}

Output result:

X =, y =

A =, B =

Ask what the underline part should be. Please complete.

Question 2: the code is as follows.

Exchg2 (int * px, int * py)

{

Int tmp = * px;

* Px = * py;

* Py = tmp;

Print ("* px = % d, * py = % d/n", * px, * py );

}

Main ()

{

Int a = 4;

Int B = 6;

Exchg2 (& a, & B );

Print ("a = % d, B = % d/n", a, B );

}

The output result is:

* Px =, * py =

A =, B =

Ask what the underline part should be. Please complete.

Question 3:

Exchg2 (int & x, int & y)

{

Int tmp = x;

X = y;

Y = tmp;

Print ("x = % d, y = % d/n", x, y );

}

Main ()

{

Int a = 4;

Int B = 6;

Exchg2 (a, B );

Print ("a = % d, B = % d/n", a, B );

}

Ii. function parameter passing method: Value passing

1. An error message about value transfer

Let's take a look at the definition of the Exchg1 function in question 1:

Void Exchg1 (int x, int y) // the x and y variables in the definition are called form parameters of the Exchg1 function.

{

Int tmp;

Tmp = x;

X = y;

Y = tmp;

Printf ("x = % d, y = % d/n", x, y)

}

Q: What do you think this function is doing?

A: Does it seem to be a logarithm of the values of x and y?

For more information, see the following procedure:

Void main ()

{

Int a = 4, B = 6;

Exchg1 (a, B) // variable a and variable B are the actual parameters of the Exchg1 function.

/Printf ("a = % d, B = % d/n", a, B)

}

Q: What will the printf ("x = % d, y = % d/n", x, y) Statement in Exchg1 () output?

I will ask again: what is the output of the printf ("a = % d, B = % d/n", a, B) statement after Exchg1?

The output result of the program is:

X = 6, y = 4

A = 4, B = 6 // Why not a = 6, B = 4?

Strange, clearly I substituted a and B into x and y respectively, and completed the exchange of two variable values in the function. Why is, the value of the B variable is still not exchanged (still a = 4, B = 6, rather than a = 6, B = 4 )? If you have this question, it is because you do not know the relationship between real parameters a and B and the form parameters x and y.

2. A preparation knowledge

To illustrate this problem, I will first provide a code:

Int a = 4;

Int x;

X =;

X = x + 3;

No. Now I ask you: what is the final a value and what is the x value?

(How to solve this problem? Give me this pediatric question. It's not easy, not just a = 4 x = 7 !)

In this Code, you need to understand that although the value is assigned to x, the variable is not an x variable. Any modification to x will not change the variable. Haha! Although simple and take it for granted, it is a very important thing to know.

3. Understand the form of value transfer

Check the code for calling the exclusive function:

Void main ()

{

Int a = 4, B = 6;

Exchg1 (a, B) // The Exchg1 function is called here.

Printf ("a = % d, B = % d", a, B)

}

The operation code completed when Exchg1 (a, B) is as follows.

Int x = a; // second

Int y = B; // ignore note that the first two rows are implicit operations when the function is called.

Int tmp;

Tmp = x;

X = y;

Y = tmp;

Note that the first two sentences are added to the call to execute the Exchg1 function:

Int x =;

Int y = B;

This is two implicit actions when a function is called. It does exist. Now I just explicitly write it out. The problem becomes clearer. (Now you think the switching operation in the function is a, B, or just x, y ?)

Originally, in fact, when the function is called, the values of the real parameters a and B are implicitly assigned to x and y respectively. Then, in the Exchg1 function you wrote, there is no a for it again, B has performed any operations. Only the x and y variables are exchanged. It Is Not a or B. Of course, the value of a and B has not changed! The function only transfers the values of a and B to x and y by assigning values. All the operations in the function are x, and the values of y are not the values of a and B. This is the so-called parameter value transfer.

Haha, finally, I understand that it is precisely because it implies the two assignment operations that lead us to the aforementioned confusion (think that a and B have replaced x, y, for x, y is an operation on a and B. This is a wrong idea !).

Iii. function parameter transfer method 2: Reference Transfer

Continue -- Address Transfer Problem!

Code for Question 2:

Exchg2 (int * px, int * py)

{

Int tmp = * px;

* Px = * py;

* Py = tmp;

Print ("* px = % d, * py = % d/n", * px, * py );

}

Main ()

{

Int a = 4;

Int B = 6;

Exchg2 (& a, & B );

Print ("a = % d, B = % d/n", a, B );

}

The output result is:

* Px = 6, * py = 4

A = 6, B = 4

Look at the Interface part of the function: Exchg2 (int * px, int * py). Note that the px and py parameters are pointers.

Let's look at the call: Exchg2 (& a, & B );

It substitutes the address of a (& a) into px, and the address of B (& B) into py. Like the above value transfer, two implicit operations are performed during the function call: The & a, & B values are assigned to px, py.

Px = &;

Py = & B;

Haha! We found that it is actually no different from passing a value, except that the address values of a and B are passed to px and py, instead of the contents of a and B, and (please make a comparison)

The entire Exchg2 function call is executed as follows:

Px = & ;//

Py = & B; // note that these two rows call the implicit action of Exchg2.

Int tmp = * px;

* Px = * py;

* Py = tmp;

Print ("* px = % d, * py = % d/n", * px, * py );

In this way, there is an implicit value assignment operation for the first two rows. We can now see that the pointer px and py values are the address values of the and B variables respectively. Next, the operation on * px and * py is of course the operation on the and B variables themselves. Therefore, the exchange in the function is the exchange of a and B values. This is the so-called address transfer (the address of a and B is transferred to px and py ).

Iv. function parameter transfer method 3: Address Transfer

Code for Question 3:

Exchg3 (int & x, int & y) // note that the format of the formal parameter at the definition is different from the value transfer

{

Int tmp = x;

X = y;

Y = tmp;

Print ("x = % d, y = % d/n", x, y );

}

Main ()

{

Int a = 4;

Int B = 6;

Exchg3 (a, B); // Note: The call method is the same as the value transfer method.

Print ("a = % d, B = % d/n", a, B );

}

Output result:

X = 6, y = 4

A = 6, B = 4 // The output result is different from the value passed.

No. The code format is different from the value transfer, that is, in the definition:

Exchg3 (int & x, int & y ).

However, we found that the values of a and B were reversed. This shows that the variables a and B are modified in Exchg3 (a, B), not just x and y.

Let's first look at Exchg3 (int & x, int & y) in the definition of the Exchg3 function ). The x and y parameters are int variables. We can call a function (for example, Exchg1 (a, B);) Like a value (for example, Exchg3 (, B );). However, there is a get address symbol in front of x and y &. With this, when you call Exchg3, the function will replace a and B with x and y respectively. We call x and y to reference the variables a and B respectively. In this way, the operation in the function is actually the real parameters a and B, that is, the function can be directly modified to the values of a and B.

Finally, compare the value transfer with the reference transfer:

1. function definition formats are different:

The value is defined as Exchg1 (int x, int y );

The reference is passed here: Exchg1 (int & x, int & y );

2. The same format is used for calling:

Value Transfer: Exchg1 (a, B );

Reference transfer: Exchg3 (a, B );

3. functions are different:

The function that passes values does not operate on the and B variables themselves, but only assigns the values a and B to x. In the y function, only the x and y variables are operated on, instead of the and B variables, the value of a and B is not modified by the Exchg1 function.

In the reference transfer Exchg3 (a, B) function, a and B are used to replace x and y respectively. The function operates on a and B.

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.