Home>

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
}

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

Thank you for pointing this out. Fixed.

Masato fromEnglish2021-12-22 19:05:37

The aFunc hasn't changed, is this as expected? ????

nekketsuuu2021-12-22 19:05:37

Thank you for pointing out again. Fixed????

Masato fromEnglish2021-12-22 19:05:37
  • Answer # 1
    As 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, thisaIs a "list", an object that holds the location of the elements in the list. For this reasonaButa_argWhether 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 handb= 1When I said, thisb bteeth1Because it is the data itself, this was copiedb_argteethb bApart from1Holds the data. For this reasonb_argEven if you updateb bWill not be updated.

    Therefore, as the questioner wants to dob bofb_argIf you want to update by writing tob bJustintThere 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 onereturnI 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:37

    No, 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 # 2
    As 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, thisaIs a "list", an object that holds the location of the elements in the list. For this reasonaButa_argWhether 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 handb= 1When I said, thisb bteeth1Because it is the data itself, this was copiedb_argteethb bApart from1Holds the data. For this reasonb_argEven if you updateb bWill not be updated.

    Therefore, as the questioner wants to dob bofb_argIf you want to update by writing tob bJustintThere 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 onereturnI 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:37

    No, 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