Measure the test taker's understanding about different types of function parameters.

Source: Internet
Author: User

We all know that the so-called program is to operate the incoming data and finally output the processed data to the user. In our programming, data is transmitted through function (or method) parameters, and then processed data is output through function return values. Data input involves passing real parameters and parameters of the function. data output involves parameters of the function return value and return value type. There is nothing special about the function return value, while others are about passing parameters through the function. Since it is about passing parameters, what has been passed in?
Function transfer participates in the assignment operation

We all know that parameter passing in a function involves the form parameter and the actual parameter, so the real parameter and the form parameter are two different variables. Therefore, the process of parameter passing through a function is similar to (or even) the process of assigning values to the form parameter. First, let's analyze the situation of passing parameters by value.

Figure 1 shows the value assignment operation diagram of a value type variable. We all know that the value type variable directly saves the content of the variable. Therefore, the assignment process is to directly copy the content saved by ValA to ValB, in this case, there is no relation between the two variables except the stored values. Therefore, changing the content of ValB does not cause ValA changes. This is the same as passing parameters by value.

 

Figure 1. Value Type Variable assignment

 

Figure 2 shows the value assignment operation for the referenced type variable. We all know that the referenced type variable stores an address, which points to the address where the variable content is located, that is to say, the referenced variable stores an address. The value assignment operation of the reference type also copies the content saved by the variable, so that both ValA and ValB point to the same address. If we modify the members of ValB, ValA will also change accordingly. We can see that the value type and the value assignment operation of the reference type are the same, both copying the content directly saved by the variable. However, if we use another object to re-assign a value to ValB, then ValB points to another object, and ValA points to the original object, which has not changed.

 

Figure 2. assign values to reference type variables

 

We know that the ref keyword is introduced in C # so that we can return the processing result of the function through function parameters. This is useful when we need to return multiple results at the same time. In fact, this is what we usually call parameter passing by reference. Figure 3 shows the variable address assignment operation. In this case, we use the new object to assign values to the object specified by ValB. ValA points to the new object. Does the ref in C # perform this operation? Let's take a look at the code at the bottom of the pipeline and the corresponding IL.

 

Figure 3. Variable address assignment

C # code

[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace FunctionParameter
{
Class PassByValue
{
Public void PassValueParameterByReference (ref ReferencePara parameter)
{
Parameter. a = 1;
Parameter = new ReferencePara ();
}
 
Public class ReferencePara
{
Public int;
Public int B;
}
}
}
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace FunctionParameter
{
Class PassByValue
{
Public void PassValueParameterByReference (ref ReferencePara parameter)
{
Parameter. a = 1;
Parameter = new ReferencePara ();
}
 
Public class ReferencePara
{
Public int;
Public int B;
}
}
}
Corresponding MSIL code

[Csharp]
// The variable address is passed.
. Method public hidebysig instance void PassValueParameterByReference (class FunctionParameter. PassByValue/ReferencePara & parameter) ASME managed
{
// Code size 17 (0x11)
. Maxstack 8
IL_0000: nop
// Load parameters
IL_0001: ldarg.1
// Load Object Reference
IL_0002: ldind. ref
IL_0003: ldc. i4.1
IL_0004: stfld int32 FunctionParameter. PassByValue/ReferencePara:
IL_0009: ldarg.1
IL_000a: newobj instance void FunctionParameter. PassByValue/ReferencePara:. ctor ()
// Reference the bound object
IL_000f: stind. ref
IL_0010: ret
} // End of method PassByValue: PassValueParameterByReference
[Csharp] view plaincopy
// The variable address is passed.
. Method public hidebysig instance void PassValueParameterByReference (class FunctionParameter. PassByValue/ReferencePara & parameter) ASME managed
{
// Code size 17 (0x11)
. Maxstack 8
IL_0000: nop
// Load parameters
IL_0001: ldarg.1
// Load Object Reference
IL_0002: ldind. ref
IL_0003: ldc. i4.1
IL_0004: stfld int32 FunctionParameter. PassByValue/ReferencePara:
IL_0009: ldarg.1
IL_000a: newobj instance void FunctionParameter. PassByValue/ReferencePara:. ctor ()
// Reference the bound object
IL_000f: stind. ref www.2cto.com
IL_0010: ret
} // End of method PassByValue: PassValueParameterByReference

 

 

 

Summary

From the beginning of our college, we started to learn the similarities and differences between the two function parameters. In fact, the process of passing parameters is the process of assigning values to the parameters, but sometimes the content of the variable is copied, and sometimes the address of the variable is copied.
Author: xuhongwei0411

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.