Question: another question on Record. overwritting Record on return

Can someone please explain in simple terms, why when I do

 r:=foo(r), where "r" is a Record, then the returned  "r" is not what is returned from foo()?  I want to write a proc foo() which takes in a Record variable, update some of its fields, and then return the updated Record to the caller

r:=Record('a','b');
foo:=proc(r)
    r:-b:=5;
    return(r);
end proc;

But now when I call the above as follows

r:-b:=99;
print(r);
r:=foo(r):
print(r);

The last print above just prints "r" and not the Record. It seems to have erased the Record.

 

But it works, if I change the name of the variable to return the result into, as 

r:-b:=99;
print(r);
r0:=foo(r):
print(r0);

When I do the same on say a Matrix, there is no problem

restart;
r:=<<1,1>>:
foo:=proc(r)
    r[1]:=5;
    return(r);
end proc;

And now

r;
r:=foo(r):
print(r);

 

Why it worked with a Matrix but not with Record? Why can't one overwrite the Record on the call return?

What would be the correct way to pass in a Record to a function, and have the function update some of its fields, and then return back the updated copy of the Record without having to make a new variable "r0" as above?

Please Wait...