In this code, the aFunc function with a list as an argument updates the value of a, but the bFunc function with an int as an argument does not update the value of b. Why is this?
Also, is there a way to return with return or update the value of b other than deleting the argument of b? Please give me some advice.
final a= [1];
var b= 1;
void aFunc (List <
int >
a_arg) {
//a.first= 3;
a_arg [0]= 3;
}
void bFunc (int b_arg) {
b_arg= 3;
}
void main () {
aFunc (a);
print (a); //[3]
bFunc (b);
print (b); //1
}
Thank you for pointing this out. Fixed.
Masato fromEnglish2021-12-22 19:05:37The aFunc hasn't changed, is this as expected? ????
nekketsuuu2021-12-22 19:05:37Thank you for pointing out again. Fixed????
Masato fromEnglish2021-12-22 19:05:37-
Answer # 1As an
image, think of the object you passed as an argument as a new copy when used in a function. Then, be aware of whether the object you are passing as an argument is the data itself or something like a pointer pointing to the location of the data.
a= [1]
When I said, thisa
Is a "list", an object that holds the location of the elements in the list. For this reasona
Buta_arg
Whether it is copied toa [0]
Whena_arg [0]
Points to the same place, soa_arg [0]
When you updatea [0]
Will also be updated.On the other hand
b= 1
When I said, thisb b
teeth1
Because it is the data itself, this was copiedb_arg
teethb b
Apart from1
Holds the data. For this reasonb_arg
Even if you updateb b
Will not be updated.Therefore, as the questioner wants to do
b b
ofb_arg
If you want to update by writing tob b
Justint
There is a way to enclose it in a class instead of making it a variable of type. There is, but in a simple example like this onereturn
I think it would be easier to understand if you use to return the result.This is the behavior that happens because his Dart employs pass-by-value for function calls. For more details, I think you can refer to the explanation in Java where the same phenomenon occurs. example: https://stackoverflow.com/q/40480/5989200
Is it okay to understand that in Dart, when List is passed as an argument, it is passed by reference, and when String and integer are passed as an argument, it is passed by value?
Masato fromEnglish2021-12-22 19:05:37No, it is more rational to interpret that I am all passing by value. There is some confusion in terms around this, but in the case of List, it would be more unified to interpret that a value called a reference (pointer) to the value is passed. (The reason I'm ambiguous in this comment is that, as far as I know, he doesn't explicitly state that it's passed by value ...)
nekketsuuu2021-12-22 19:05:37 -
Answer # 2As an
image, think of the object you passed as an argument as a new copy when used in a function. Then, be aware of whether the object you are passing as an argument is the data itself or something like a pointer pointing to the location of the data.
a= [1]
When I said, thisa
Is a "list", an object that holds the location of the elements in the list. For this reasona
Buta_arg
Whether it is copied toa [0]
Whena_arg [0]
Points to the same place, soa_arg [0]
When you updatea [0]
Will also be updated.On the other hand
b= 1
When I said, thisb b
teeth1
Because it is the data itself, this was copiedb_arg
teethb b
Apart from1
Holds the data. For this reasonb_arg
Even if you updateb b
Will not be updated.Therefore, as the questioner wants to do
b b
ofb_arg
If you want to update by writing tob b
Justint
There is a way to enclose it in a class instead of making it a variable of type. There is, but in a simple example like this onereturn
I think it would be easier to understand if you use to return the result.This is the behavior that happens because his Dart employs pass-by-value for function calls. For more details, I think you can refer to the explanation in Java where the same phenomenon occurs. example: https://stackoverflow.com/q/40480/5989200
Is it okay to understand that in Dart, when List is passed as an argument, it is passed by reference, and when String and integer are passed as an argument, it is passed by value?
Masato fromEnglish2021-12-22 19:05:37No, it is more rational to interpret that I am all passing by value. There is some confusion in terms around this, but in the case of List, it would be more unified to interpret that a value called a reference (pointer) to the value is passed. (The reason I'm ambiguous in this comment is that, as far as I know, he doesn't explicitly state that it's passed by value ...)
nekketsuuu2021-12-22 19:05:37
- flutter : how to switch locale programmatically?
- android - i want to divide animation into different classes [flutter]
- android - coordinates of quadraticbezierto [flutter]
- android - meaning of , necessity [flutter]
- android studio - general search functions:i want to link query and database
- i can't get spaces in the characters in a field in my firebase database
- android - cannot use fade animation [dart package]
- android - where _pagecontrollerpage points [flutter]
- android - hide status bar [system chrome]
- android - animated builder does not work [flutter]
Are you mistaken for a and a_arg and b and b_arg in aFunc and bFunc? In this program print (b) prints 3.
nekketsuuu2021-12-22 19:05:37