Home > Software engineering >  Who can explain to me why the final end C=2 D will be equal to 4 clearly said the body of the functi
Who can explain to me why the final end C=2 D will be equal to 4 clearly said the body of the functi

Time:09-16

The following script runs after each variable's value is how much?
A=1
B=2
C=3
D=4
E=function (a, b)
The Function Function (a, b)
Function=a + b * a - b
C=b
D=a
End the Function
Answer: A=1, B=2, C=2, D=1, E=1
` ` ` who can explain to me why the final end C=2 D will be equal to 4 clearly said the body of the function will not change the value of the main program

CodePudding user response:

Because VB parameters have difference between byval and byref
Byval namely pass values to the function, you can understand for the parameter variables are copied a to the function, the next function on how the parameters change will not affect the original variables,
Byref variables passed is a reference, you can understand as to pass the address of external variables to the function, the function of all is in the external memory address directly modified, so when the function exits when external variables will also change,
VB for byref parameters in the default transfer way, to change is explicitly declared byval parameters,

CodePudding user response:

The
refer to the original poster qq_33906416 response:
the following script after the operation of each variable value is how much?
A=1
B=2
C=3
D=4
E=function (a, b)
The Function Function (a, b)
Function=a + b * a - b
c=
D=a

End the Function
Answer: A=1, B=2, C=2, D=1, E=1
` ` ` who can explain to me why the final end C=2 D will be equal to 4 clearly said the body of the function will not change the value of the main program of


Add the red part of the assignment statement, isn't it? Since assigning values, how can the same?

CodePudding user response:

Where said "function body will not change the value of the main program"? Don't understand, if the body does not change the value of the main program, why set the global variable? Don't just read?

CodePudding user response:

reference 3 floor response: VB amateurs
where said "function body will not change the value of the main program"? Don't understand, if the body does not change the value of the main program, why set the global variable? Don't just read?


Instructions for the parameters of the program or you don't understand, when the parameters of the various languages to have the value and the reference in two ways, but without expression, such as wear ordinary C variables and pass a pointer, byval and byref in VB,
Principle 1/f, said the above, see the effect directly to the code here

 
Option Explicit

Private Sub test (As an Integer, a ByRef b As an Integer, ByVal c As an Integer, ByRef d As an Integer)
The Debug. Print "into the parameter values within the function:"; a; b; c; D
A=a + 1
B=b + 2
C=c + 3
Dim d1 As Integer
D1=d
D1 d1 + 4=
The Debug. Print "function after the internal processing:"; a; b; c; d; "D1 within the function:"; D1

End Sub

Private Sub Command1_Click ()
As Integer Dim a, b As an Integer, As an Integer c, d As Integer
A=100: b=200: c=300: d=400
The Debug. Print "before the function call into the parameter values:"; a; b; c; D
Test a, b, c, d
The Debug. Print "into the parameter values after the function call:"; a; b; c; D
'a default byref will change, b explicitly declared byref will change, C explicitly declared byval will not change, d declare byref but using the function of the internal variables to copy a data processing, and hasn't changed into the address data on the refs
End Sub

CodePudding user response:

references 4 floor crispy ice cream response:
Quote: refer to the third floor response: VB amateurs

Where said "function body will not change the value of the main program"? Don't understand, if the body does not change the value of the main program, why set the global variable? Don't just read?


Instructions for the parameters of the program or you don't understand, when the parameters of the various languages to have the value and the reference in two ways, but without expression, such as wear ordinary C variables and pass a pointer, byval and byref in VB,
Principle 1/f, said the above, see the effect directly to the code here

 
Option Explicit

Private Sub test (As an Integer, a ByRef b As an Integer, ByVal c As an Integer, ByRef d As an Integer)
The Debug. Print "into the parameter values within the function:"; a; b; c; D
A=a + 1
B=b + 2
C=c + 3
Dim d1 As Integer
D1=d
D1 d1 + 4=
The Debug. Print "function after the internal processing:"; a; b; c; d; "D1 within the function:"; D1

End Sub

Private Sub Command1_Click ()
As Integer Dim a, b As an Integer, As an Integer c, d As Integer
A=100: b=200: c=300: d=400
The Debug. Print "before the function call into the parameter values:"; a; b; c; D
Test a, b, c, d
The Debug. Print "into the parameter values after the function call:"; a; b; c; D
'a default byref will change, b explicitly declared byref will change, C explicitly declared byval will not change, d declare byref but using the function of the internal variables to copy a data processing, and hasn't changed into the address data on the refs
End Sub


I know the difference between byval and byref, post function inside of the building Lord incoming parameters is a and b, a and b value is unchanged, he asked is why c and d, c and d is not the incoming parameters, in this place is not a global variable?

CodePudding user response:

The building Lord, is you need to know the concept of "variable scope"...

You say "the body of the function will not change the value of the main program", are generally not such claims,
May be you to understand "what someone was saying" is wrong,
So you come here a a bit upside down self depreciatory expression,

CodePudding user response:

refer to 6th floor a toast to invite the bright moon response:
the building Lord, is you need to know the concept of "variable scope"...

You say "the body of the function will not change the value of the main program", are generally not such claims,
May be you to understand "what someone was saying" is wrong,
So you come here a a bit upside down self depreciatory expression,


it is for this reason

CodePudding user response:

Function=a + b * a - b function=1 + 2 * 1 or 2, 1 of course
C=b to b value assigned to the c, c, of course, is 2
D=a will be a value assigned to d, d, of course, is the 1

Therefore, after the called function: A=1, B=2, C=2, D=1, E=1 no problem ah,
  • Related