Home > front end >  Of new, how the global variable substitution parameters
Of new, how the global variable substitution parameters

Time:10-10

Problem as shown, the key part for circle
Before this function defined var sitousu=0;
But after this function performs sitousu did not change
Sincerely ask

CodePudding user response:

Breaking point with the, may be your conditions are not met, did not perform this step c + +

CodePudding user response:

Because of your global variables is the basic type of c, is in the stack, you pass the current parameter c, equivalent to the c to assign a value to the parameter, so no matter how c in function change will not affect global c,
If you want to change the global variable in the function in parameter passing way, you can change the global variable c to a reference type (such as: array, object, etc.) these reference type is the inside of the heap, as parameters when the pointer to the current reference objects assigned to the parameter, when change the parameters in the function, the change is the pointer points to object (that is, the value of the object), at this time of global variables will also change,
Conclusion: basic variable when a parameter by value, function change will not affect the original value; Reference object when the parameter value, function change, the original cost will change,

CodePudding user response:

refer to the second floor XZZFXZ response:
because your global variables, c is the basic type, is in the stack, you pass the current parameter c, equivalent to the c to assign a value to the parameter, so no matter how c in function change will not affect global c,
If you want to change the global variable in the function in parameter passing way, you can change the global variable c to a reference type (such as: array, object, etc.) these reference type is the inside of the heap, as parameters when the pointer to the current reference objects assigned to the parameter, when change the parameters in the function, the change is the pointer points to object (that is, the value of the object), at this time of global variables will also change,
Conclusion: basic variable when a parameter by value, function change will not affect the original value; Reference object when the parameter value, function change, with the change of original value and the

Thank you, your answer to a lot of help, baidu today a lot of the method of reference types, try many times still won't do, can consult the specific operation method, question plagued me for the whole day, sincerely

CodePudding user response:

The easiest way is, the parameters of the function with a return back, again after the function call for global variables to assignment for the function return value,
For example:
 
Var num=1;

The function fun (c) {
C + +;
return c;
}

Num=fun (num);
Alert (num);

CodePudding user response:

reference weixin_45991172 reply: 3/f
Quote: refer to the second floor XZZFXZ response:
because your global variables, c is the basic type, is in the stack, you pass the current parameter c, equivalent to the c to assign a value to the parameter, so no matter how c in the function change will not affect the global c,
If you want to change the global variable in the function in parameter passing way, you can change the global variable c to a reference type (such as: array, object, etc.) these reference type is the inside of the heap, as parameters when the pointer to the current reference objects assigned to the parameter, when change the parameters in the function, the change is the pointer points to object (that is, the value of the object), at this time of global variables will also change,
Conclusion: basic variable when a parameter by value, function change will not affect the original value; Reference object when the parameter value, function change, with the change of original value and the

Thank you, your answer to a lot of help, baidu today a lot of the method of reference types, try many times still won't do, can consult the specific operation method, question plagued me for the whole day, sincerely ask


A simple chestnuts:
 
//basic types - number, string, Boolean, null, and undefined, symbol
let a=1;

The function changeA (val) {
Val +=1;
};
The console. The log (" a="+ a);//a=1
ChangeA (a);
The console. The log (" a="+ a);//a=1

//reference types - array, object, function
Let b=[1, 2, 3].

The function changeB (arr) {
Arr [0]=4;
}
The console. The log (b)//[1, 2, 3]
ChangeB (b);
The console. The log (b)//[4, 2, 3]

CodePudding user response:

reference 5 floor XZZFXZ reply:
Quote: refer to the third floor weixin_45991172 response:

Quote: refer to the second floor XZZFXZ response:
because your global variables, c is the basic type, is in the stack, you pass the current parameter c, equivalent to the c to assign a value to the parameter, so no matter how c in function change will not affect global c,
If you want to change the global variable in the function in parameter passing way, you can change the global variable c to a reference type (such as: array, object, etc.) these reference type is the inside of the heap, as parameters when the pointer to the current reference objects assigned to the parameter, when change the parameters in the function, the change is the pointer points to object (that is, the value of the object), at this time of global variables will also change,
Conclusion: basic variable when a parameter by value, function change will not affect the original value; Reference object when the parameter value, function change, with the change of original value and the

Thank you, your answer to a lot of help, baidu today a lot of the method of reference types, try many times still won't do, can consult the specific operation method, question plagued me for the whole day, sincerely ask


A simple chestnuts:
 
//basic types - number, string, Boolean, null, and undefined, symbol
let a=1;

The function changeA (val) {
Val +=1;
};
The console. The log (" a="+ a);//a=1
ChangeA (a);
The console. The log (" a="+ a);//a=1

//reference types - array, object, function
Let b=[1, 2, 3].

The function changeB (arr) {
Arr [0]=4;
}
The console. The log (b)//[1, 2, 3]
ChangeB (b);
The console. The log (b)//[4, 2, 3]

Thank you very much, probably understand.
  • Related