Discussion:
handling of ambiguities
(too old to reply)
Thomas Plehn
2007-07-24 10:55:16 UTC
Permalink
Hello,

how does octave handle ambiguities in function calls such as in this example

function y = do_something(x,y,foo)

printf("x = %i, y = %i", x, y);

mult = x * y;

y = mult;

endfunction

how does octave know, which y must be used?
The result of this function is that the parameters x and y are printed,
then multiplicated and then assigned to the return value of the function.
So everything works as it was meant to do.
If one would use any real compiled language, one would expect an
ambiguity error at compile time, but octave doesn't produce an error
neither in the compiling stage nor in the runtime stage.
And the best: Things are done as they were meant to be done.
highegg
2007-07-24 11:59:08 UTC
Permalink
Post by Thomas Plehn
Hello,
how does octave handle ambiguities in function calls such as in this example
function y = do_something(x,y,foo)
printf("x = %i, y = %i", x, y);
mult = x * y;
y = mult;
endfunction
how does octave know, which y must be used?
The result of this function is that the parameters x and y are printed,
then multiplicated and then assigned to the return value of the function.
So everything works as it was meant to do.
If one would use any real compiled language, one would expect an
ambiguity error at compile time, but octave doesn't produce an error
neither in the compiling stage nor in the runtime stage.
And the best: Things are done as they were meant to be done.
Although I have not checked this thoroughly, the rule is not hard to
guess:
At entry to the function, Octave supplies the second argument in
the local variable y.
At exit from the function, Octave reads the result from the local
variable y.

This means that this function:

function x = copy(x)
[endfunction]

just returns its argument, even if the function body is empty.

cheers,
Jaroslav
David Bateman
2007-07-25 22:40:04 UTC
Permalink
Post by Thomas Plehn
Hello,
how does octave handle ambiguities in function calls such as in this example
function y = do_something(x,y,foo)
printf("x = %i, y = %i", x, y);
mult = x * y;
y = mult;
endfunction
how does octave know, which y must be used?
The result of this function is that the parameters x and y are printed,
then multiplicated and then assigned to the return value of the function.
So everything works as it was meant to do.
If one would use any real compiled language, one would expect an
ambiguity error at compile time, but octave doesn't produce an error
neither in the compiling stage nor in the runtime stage.
And the best: Things are done as they were meant to be done.
Octave and Matlab are a pass by value language rather than a pass by
reference language as programmers in C or C++ are more use to. That
means that the arguments passed to a function are copied and so the only
way to alter an argument returned by a function is assigning it on the
LHS of the function.

This means that in your above example y is a copy of the value passed,
and so also using it as the return value is not in any way ambiguous.
For further clarification, consider the function.

function y = foo (x)
x(1) = 1;
y = x;
end

Then doing

x = [2,3,4];
y = foo(x)
disp(x)

you'll see that your original x value is not altered in any way.

Note that the copy is low cost as Octave and Matlab both use a copy on
write scheme. The copy made when passing to a function just increments a
reference counter. Above a real copy of y is never made as you assign a
new value to y..If instead you did something like

y(1,:) = mult;

a copy of the y passed value would have been made before the subsasgn
operation.

D.

Loading...