编程业余爱好者..
我也百度搜了,但是说的我也看不明白,都是专业属于...
别鄙视我,给我举个简单的例子呗.
PHPz2017-04-17 14:30:22
Sub Foo(ByVal p1, ByRef p2)
p1 = 9
p2 = 9
End Sub
Dim a As Integer
Dim b As Integer
a = 1 : b = 2
Foo(a, b)
' 此时 a 的值不变,仍为 1
' b 的值变为 9
The "formal parameters" here are p1
and p2
, and the "actual parameters" are a
and b
. When
calls Foo
, the system copies a copy of a
and passes it to p1
, so changing the value of p1
does not affect a
. And p2
represents a reference to the b
variable. Changing p2
will also cause b
to change.
It’s been a long time since I wrote VB. Please correct me if I’m wrong.
高洛峰2017-04-17 14:30:22
Sub Foo(ByVal p1, ByRef p2)
p1 + p2
End Sub
Formal parameters are preset parameters that can be passed in. Foo declares that it accepts two parameters, p1 and p2. At this time, p1 and p2 are actually only ideal states, not actual parameters, so they are called formal parameters
Foo(1, 2)
When calling Foo, two actual and meaningful parameters are passed in. 1 and 2 are actual parameters, so they are called actual parameters
迷茫2017-04-17 14:30:22
First understand the formal parameters and actual parameters. The formal parameters are the parameters in parentheses when defining the function, and the actual parameters are the parameters actually passed in when using this function.
Understand again that variables are stored in memory. Each variable has its own independent memory space. Each memory space has an address. This memory can be found through the address.
Then look at these two English meanings, ByVal: pass by value, ByRef: pass by address.
What is pass by value? That is to first open up a new memory space for the formal parameters, and then transfer the content of the actual parameters into this new memory space. After this transfer, the formal and actual parameters are in two different memory spaces, which means that they are independent of each other. Yes, how do you toss this formal parameter in the function? What you are tossing is this temporary memory space. It has nothing to do with the actual parameters. It is lying quietly in its own cabin.
What is delivery by address? That is to directly pass the memory space address of the actual parameter to the formal parameter. In this way, the formal parameter and the actual parameter share a memory. Therefore, changes in the content of the formal parameter also directly change the content of the actual parameter. If you toss the formal parameter in the function, it is equivalent to In tossing the actual parameters.
So under normal circumstances, functions will have a return, because functions are passed by value by default. If they do not return, after the function is executed, the temporary memory space will be released, and nothing will happen after half a day of execution. Of course, if If you define delivery by address, there is no need to return.