help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problems


From: James Sherman Jr.
Subject: Re: problems
Date: Thu, 1 Oct 2015 06:57:49 -0400

On Tue, Sep 29, 2015 at 1:20 PM, Robert Setif <address@hidden> 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  address@hidden
>
> _______________________________________________
> Help-octave mailing list
> address@hidden
> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]