I have a confusion regarding passing list to a function in dart
here i have made two example to clear my query
in first example
when i passed a list to function and add some new values to list…it updates void main’s list. thats fine like passing byref
but in another example,
when i pass a list to function and applying him brand new list ..it does not update void main’s list..why?
where should i get documentation regarding this…its long time i am confused in byval and byref while passing list or object
void update(List<String> list)
{
list.add('C');
}
void main()
{
List<String> list=['A','B'];
print('Before'+list.toString());
update(list);
print('after'+list.toString());
}
//Before[A, B] after[A, B, C] means updating void main's list
Program 2
void update(List<String> list)
{
List<String> newList=['x','y'];
list=newList;
}
void main()
{
List<String> list=['A','B'];
print('Before'+list.toString());
update(list);
print('after'+list.toString());
}
//Before[A, B] after[A, B] here not updating void main's list, and why?
5
Answers
In the second example, you’re affecting locally the value of your parameter "list" a new value within the scope of your function because list are passed by reference to functions. In order for this to work you would either :
need to make your update function return a new list that you affect to your list in the main function
use .addAll() / .clear() methods on the list given as parameter of your updateFunction
First solution
Second solution
In Dart, when you pass an object (including lists) to a function, you are passing a reference to the object. Modifying the object via this reference will reflect outside the function. However, if you reassign a new object to the parameter, it only changes the local reference within the function and does not affect the original reference.
I will explain you in detail
myNumbers is a list containing
[1, 2]
.appendToList
is called withmyNumbers
as an argument.Inside
appendToList
, theadd
method modifies the original list byadding
3
.The modification is reflected in the
myNumbers
list inmain
.myNumbers is a list containing
[1, 2]
.replaceList
is called withmyNumbers
as an argument.Inside
replaceList
, a new list[7, 8]
is created and assigned to thelocal variable
numbers
.This reassignment does not affect
myNumbers
inmain
because it onlychanges the local reference inside the
replaceList
function.Copy of a reference variable (pointer) is passed to parameter
Example 1 is already understood, let’s explain exmple2:
when you are calling that function like that:
update(list);
a copy of the reference variable
list
is passed to the parameterupdate(List<String> list)
list
in the method signature.an image holds a thousand words, which leads to that:
Ok, your program now has two reference variables which are pointing to the same memory location, imagine you have done this:
your local list variable (in update function) will not change that list, instead it will point to another one
['x' , 'y']
:which leads to the following:
and the list which is defined in the main still the same.
your confusion is right but just keep in mind that while giving brand new object or brand new list to passed argument it will not affect original list or original object,
but while adding or deleting list member or object’s property it will update original list..
In Short ,
consider as by value while giving brand new list or new object to passed argument // will not effect original list
and
consider as by reference while just adding or updating its member
// will effect original list
Exmaple 1:
Example : 2