function y = g(x)
x.Color = 'blue';
y = x;
end
y = g(obj);
The function g modifies its copy of the input object and returns that copy, but does not
change the original object.
y.Color
ans =
blue
obj.Color
ans =
red
If the function g did not return a value, the modification of the object Color property
would have occurred only on the copy of obj within the function workspace. This copy
would have gone out of scope when the function execution ended.
Overwriting the original variable actually replaces it with a new object:
obj = g(obj);
Passing Handle Objects
When you pass a handle to a function, the function makes a copy of the handle variable,
just like when passing a value object. However, because a copy of a handle object refers
to the same object as the original handle, the function can modify the object without
having to return the modified object.
For example, suppose you modify the SimpleClass class definition to make a class
derived from the handle class:
classdef SimpleHandleClass < handle
properties
Color
end
methods
Comparison of MATLAB and Other OO Languages