Pointers and references: Difference between revisions

Content added Content deleted
m (Added Sidef language)
(→‎{{header|ooRexx}}: add Use Arg to call by reference)
Line 729: Line 729:
ooRexx is call-by-value. When passing arguments, you are passing object references by value. There is no such thing as "passing an object" as objects are not values in the language. As noted above, if the object that the reference is pointing to is mutable, then it is possible to modify that object's state through the reference. Then if the calling function has a reference to the same object, they will see that modification through the reference. So if you want to reflect changes in an argument back to the caller, one thing that you can do is wrap the argument as an attribute of an object, then modify the object through the reference.
ooRexx is call-by-value. When passing arguments, you are passing object references by value. There is no such thing as "passing an object" as objects are not values in the language. As noted above, if the object that the reference is pointing to is mutable, then it is possible to modify that object's state through the reference. Then if the calling function has a reference to the same object, they will see that modification through the reference. So if you want to reflect changes in an argument back to the caller, one thing that you can do is wrap the argument as an attribute of an object, then modify the object through the reference.


With the Use Arg instrucion, a subroutine get access to the caller's argument object by reference:
<lang oorexx>
a.='not set'
a.3=3
Say 'Before Call sub: a.3='a.3
Call sub a.
Say ' After Call sub: a.3='a.3
Call sub2 a.
Say ' After Call sub2: a.3='a.3
Exit

sub: Procedure
Use Arg a. -- this established access to the caller's object
a.3=27
Return

sub2: Procedure
Parse Arg a. -- this gets the value of the caller's object
a.3=9
Say 'in sub2: a.='a.
Say 'in sub2: a.3='a.3 -- this changes the local a.
Return</lang>
{{out}}
<pre>Before Call sub: a.3=3
After Call sub: a.3=27
in sub2: a.=not set
in sub2: a.3=9
After Call sub2: a.3=27</pre>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==