On Tue, Sep 29, 2015 at 1:20 PM, Robert Setif <

[hidden email]> wrote:

> I would like to know why the file:

> "

> # hyp.m

> function r=hyp(x,y)

> # hypoténuse d'un triangle de côtés x et y

> r=sqrt(x*x+y*y)

> endfunction

> "

> always alert :

> "

> >> hyp

> error: 'x' undefined near line 4 column 8

> error: called from

> hyp at line 4 column 2

> error: evaluating argument list element number 1

> error: called from

> hyp at line 4 column 2

> "

> Fortunately, this is not a crashing error.

> That problem happens with all my files.

> I do not know to declare in function-arguments a variable,

> for instance x as real, y as integer, m as scalar.

>

> I also would like to change at the screen the shape of result

> from a "for i=1:30,disp(i^2),endfor" in order to

> request in a list-shape rather that column-shape,

> (with "disp" or another command).

> Regards

> Robert SETIF

[hidden email]
>

> _______________________________________________

> Help-octave mailing list

>

[hidden email]
>

https://lists.gnu.org/mailman/listinfo/help-octave>

Hi Robert,

You're getting 'x' undefined, because you're not passing in values to

your function. When you define a function, say

------

function value = func(x,y,z)

value = x*y+z;

endfunction

------

what the first line indicates is the number of inputs and outputs that

your function expects. In my case, it expects 3 inputs (or to be

specific, at most 3 inputs can passed to it. There can be less). In

your case, it expects 2 inputs, x and y.

However, when you call your function hyp, you don't pass in any values

to it, so when the function is called, there are no values that Octave

can assign to x or y. Thus, when you try to calculate the length of

the hypotenuse, Octave raises the error, 'x' is undefined. ('y' is

also undefined, but since x appears first, its the first error it

encounters).

This should be alleviated by passing x and y when you call the

function, such as:

> hyp(3,4)

Then this should return 5.

Hope this helps,

James Sherman

_______________________________________________

Help-octave mailing list

[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave