function help

classic Classic list List threaded Threaded
29 messages Options
12
Reply | Threaded
Open this post in threaded view
|

function help

Tom Bohdan

I have had no success with finding why the following doesn't work. I have tried many examples in Octave's Help and they don't work either.


Why is it that if you type each line into the command line then the following works:


function total=addr(x,y)

total=x+y;

endfunction


add(5,4)


>ans=9


but if i put the above lines into a m-file then the interpreter spits out x undefined etc.


This happens for all the functions i have found in the LSODE examples etc. If you type them one by one at the command line it works but not in a script file. What am i missing ???


regards

Tom


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

Doug Stewart-4


On Thu, Feb 23, 2012 at 4:26 AM, Tom Bohdan <[hidden email]> wrote:

I have had no success with finding why the following doesn't work. I have tried many examples in Octave's Help and they don't work either.


Why is it that if you type each line into the command line then the following works:


function total=addr(x,y)

total=x+y;

endfunction


add(5,4)


>ans=9


but if i put the above lines into a m-file then the interpreter spits out x undefined etc.


This happens for all the functions i have found in the LSODE examples etc. If you type them one by one at the command line it works but not in a script file. What am i missing ???


regards

Tom


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave



It works fine for me.

What os 
what ver. of octave
Are you sure you don't have a capital X mixed with a small x?

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

Juan Pablo Carbajal


On Thu, Feb 23, 2012 at 4:10 PM, Doug Stewart <[hidden email]> wrote:


On Thu, Feb 23, 2012 at 4:26 AM, Tom Bohdan <[hidden email]> wrote:

I have had no success with finding why the following doesn't work. I have tried many examples in Octave's Help and they don't work either.


Why is it that if you type each line into the command line then the following works:


function total=addr(x,y)

total=x+y;

endfunction


add(5,4)


>ans=9


but if i put the above lines into a m-file then the interpreter spits out x undefined etc.


This happens for all the functions i have found in the LSODE examples etc. If you type them one by one at the command line it works but not in a script file. What am i missing ???


regards

Tom


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave



It works fine for me.

What os 
what ver. of octave
Are you sure you don't have a capital X mixed with a small x?

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave


Are you saving the m-file sin the directory that is shown when you run the command
> pwd
in the Octave terminal?



--
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

bpabbott
Administrator
In reply to this post by Tom Bohdan
On Feb 23, 2012, at 4:26 AM, Tom Bohdan <[hidden email]> wrote:

I have had no success with finding why the following doesn't work. I have tried many examples in Octave's Help and they don't work either.


Why is it that if you type each line into the command line then the following works:


function total=addr(x,y)

total=x+y;

endfunction


add(5,4)


>ans=9


but if i put the above lines into a m-file then the interpreter spits out x undefined etc.


This happens for all the functions i have found in the LSODE examples etc. If you type them one by one at the command line it works but not in a script file. What am i missing ???


regards

Tom


Did you intend "add()" or "addr()" ?


Ben


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

damian.harty
I've tried your example and it works for me, but I have noticed this business of functions is a little perplexing. For sure if you type the stuff in from the command line then it works just fine (notwithstanding the feral "r" that crept in there).

If you want to put it into an m file, you need two separate files. I have add.m :
function total=add(x,y)
  total=x+y;
endfunction

and test_function.m :
answer=add(5,4)

And then it works. I fail at every attempt to combine them into a single file...

Damian
Senior Research Fellow - Vehicle & System Dynamics Coventry University United Kingdom
Reply | Threaded
Open this post in threaded view
|

Re: function help

bpabbott
Administrator
In reply to this post by bpabbott

On Feb 23, 2012, at 10:47 AM, Ben Abbott wrote:

> On Feb 23, 2012, at 4:26 AM, Tom Bohdan <[hidden email]> wrote:
>
>> I have had no success with finding why the following doesn't work. I have tried many examples in Octave's Help and they don't work either.
>>
>> Why is it that if you type each line into the command line then the following works:
>>
>> function total=addr(x,y)
>> total=x+y;
>> endfunction
>>
>> add(5,4)
>>
>> >ans=9
>>
>> but if i put the above lines into a m-file then the interpreter spits out x undefined etc.
>>
>> This happens for all the functions i have found in the LSODE examples etc. If you type them one by one at the command line it works but not in a script file. What am i missing ???
>>
>> regards
>> Tom
>
> Did you intend "add()" or "addr()" ?
>
> Ben

Note, that if the lines below are in add.m, then add(x,y) calls that function.

        function total=addr(x,y)
                total=x+y;
        endfunction

If the same lines are typed at the command prompt then add(x,y) is calling something else ... likely a function saved in a file with the name add.m.

You can check by typing ...

        which add

Ben

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

martin_helm
In reply to this post by damian.harty
Am 23.02.2012 20:55, schrieb damian.harty:
> And then it works. I fail at every attempt to combine them into a
> single file...
>

Because by design all the additional function definitions in the .m file
are local and hidden to the outside world. You can of course make them
visible with function handles like this

--- multif.m ---

function f = multif(name)
  if strcmp(name, "a")
    f = @__a__;
  else
    f = @__b__;
  endif
endfunction

function y= __a__(x)
  y=sin(x);
endfunction

function y=__b__(x)
  y=cos(x);
endfunction

--- end multif.m ---

octave:1> f = multif("a")
f = @__a__
octave:2> f(2)
ans =  0.90930
octave:3> f = multif("b")
f = @__b__
octave:4> f(2)
ans = -0.41615


But that's a bit off topic here.


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

Jordi Gutiérrez Hermoso-2
On 23 February 2012 15:15, Martin Helm <[hidden email]> wrote:
> Am 23.02.2012 20:55, schrieb damian.harty:
>> And then it works. I fail at every attempt to combine them into a
>> single file...
>>
>
> Because by design all the additional function definitions in the .m file
> are local and hidden to the outside world. You can of course make them
> visible with function handles like this

That's an ugly an unnecessary Matlab-inspired hack.

In Octave, unlike in Matlab, you can define functions in the
interpreter and in script files.

So you can do something like writing this in func_init.m:

    1;
    function out = foo(x)
       ## ...
    endfunction

    function out = bar(x)
       ## ...
    endfunction

And whenever you write "func_init" both foo and bar functions will be
defined in scope.

Subfunctions are still hidden like in Matlab, but at least you don't
need to be doing uglier function handle hacks to just get a few
functions into scope.

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

martin_helm
Am 23.02.2012 21:27, schrieb Jordi Gutiérrez Hermoso:
> That's an ugly an unnecessary Matlab-inspired hack.
No idea, I do not use Matlab. It is inspired by lisps local functions
returned by a dispatch from a closure and it is for sure no hack.

> In Octave, unlike in Matlab, you can define functions in the
> interpreter and in script files.
>
> So you can do something like writing this in func_init.m:
>
>     1;
>     function out = foo(x)
>        ## ...
>     endfunction
>
>     function out = bar(x)
>        ## ...
>     endfunction
>
> And whenever you write "func_init" both foo and bar functions will be
> defined in scope.
I consider that a hack because you execute a script instead of using a
dispatch, though I sometimes use it myself.
> Subfunctions are still hidden like in Matlab, but at least you don't
> need to be doing uglier function handle hacks to just get a few
> functions into scope.
>

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

damian.harty
In reply to this post by bpabbott
It's not calling another function called add, this is pretty easily verified by renaming the file add.m to banana.m, doing a clear all and then trying it again.

Damian
Senior Research Fellow - Vehicle & System Dynamics Coventry University United Kingdom
Reply | Threaded
Open this post in threaded view
|

Re: function help

damian.harty
In reply to this post by martin_helm
martin_helm wrote
Because by design all the additional function definitions in the .m file are local and hidden to the outside world.
Well, that's the bit I'm not getting. If they are in the script file I'm working in, they don't get much more local than that, do they? I can understand why they would be invisible, say, from the command line despite being present in a script, but I can't understand why they aren't visible when they are in this file and why instead they have to be in a file with their own name.

Nevertheless, that's how it is, and I need separate files for any functions that I write or else do that init thing. I'll live. But it ain't about "local"...

Damian
Senior Research Fellow - Vehicle & System Dynamics Coventry University United Kingdom
Reply | Threaded
Open this post in threaded view
|

Re: function help

Jordi Gutiérrez Hermoso-2
On 23 February 2012 15:46, damian.harty <[hidden email]> wrote:
> Well, that's the bit I'm not getting. If they are in the script file I'm
> working in, they don't get much more local than that, do they? I can
> understand why they would be invisible, say, from the command line despite
> being present in a script, but I can't understand why they aren't visible
> when they are /in this file/ and why instead they /have to be in a file with
> their own name/.

They should be visible from inside the same file. That's called a
subfunction. What exactly are you putting inside your m-file?

Are you aware of the distinction between script files and function
files? They're both m-files, but they behave quite differently:

    http://www.gnu.org/software/octave/doc/interpreter/Functions-and-Scripts.html

HTH,
- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

bpabbott
Administrator
In reply to this post by damian.harty
On Feb 23, 2012, at 3:46 PM, "damian.harty" <[hidden email]> wrote:

> martin_helm wrote
>>
>> Because by design all the additional function definitions in the .m file
>> are local and hidden to the outside world.
>>
>
> Well, that's the bit I'm not getting. If they are in the script file I'm
> working in, they don't get much more local than that, do they? I can
> understand why they would be invisible, say, from the command line despite
> being present in a script, but I can't understand why they aren't visible
> when they are /in this file/ and why instead they /have to be in a file with
> their own name/.
>
> Nevertheless, that's how it is, and I need separate files for any functions
> that I write or else do that init thing. I'll live. But it ain't about
> "local"...
>
> Damian

If a function in a *script* is named "addr()" and you call "add()" you're calling something other than the function you named addr

Ben
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

bpabbott
Administrator
In reply to this post by damian.harty
On Feb 23, 2012, at 3:46 PM, "damian.harty" <[hidden email]> wrote:

>
> martin_helm wrote
>>
>> Because by design all the additional function definitions in the .m file
>> are local and hidden to the outside world.
>>
>
> Well, that's the bit I'm not getting. If they are in the script file I'm
> working in, they don't get much more local than that, do they? I can
> understand why they would be invisible, say, from the command line despite
> being present in a script, but I can't understand why they aren't visible
> when they are /in this file/ and why instead they /have to be in a file with
> their own name/.
>
> Nevertheless, that's how it is, and I need separate files for any functions
> that I write or else do that init thing. I'll live. But it ain't about
> "local"...
>
> Damian

Maybe you can attach the actual script so that others can attempt to duplicate what you are seeing ?

Ben
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

bpabbott
Administrator
In reply to this post by Tom Bohdan
On Feb 23, 2012, at 5:56 PM, Tom Bohdan wrote:

> --- On Fri, 24/2/12, Ben Abbott <[hidden email]> wrote:
>
>> On Feb 23, 2012, at 4:26 AM, Tom Bohdan <[hidden email]> wrote:
>>
>>> I have had no success with finding why the following doesn't work. I have tried many examples in Octave's Help and they don't work either.
>>>
>>> Why is it that if you type each line into the command line then the following works:
>>>
>>> function total=addr(x,y)
>>> total=x+y;
>>> endfunction
>>>
>>> add(5,4)
>>>
>>> >ans=9
>>>
>>> but if i put the above lines into a m-file then the interpreter spits out x undefined etc.
>>>
>>> This happens for all the functions i have found in the LSODE examples etc. If you type them one by one at the command line it works but not in a script file. What am i missing ???
>>>
>>> regards
>>> Tom
>>
>> Did you intend "add()" or "addr()" ?
>>
>> Ben
>
> thanks for your prompt reply.
> Another person suggested that it was because there may be a function called add() that may be messing it up but i retried the following and got the same problems:
>
> function total=exxample(x,y)
> total=x+y;
> endfunction
> exxample(9,8)
>
> At home i use Ubuntu 10.10 LTS which has octave 2.3.3. I typically use QT octave but i tried it on the command line of Octave itself too.
>
> At work i use Windows Xp and QT Octave and Octave 2.3.4. Again similar issues BUT i did have some success.
>
> if i hit Run within the editor of QT octave on the Windows version only the script worked !
>
> If i run it by the Command line in the dos window it failed exactly like the Linux versions with undefined x in row2, col7 or something like that.
>
> If its typed line -by-line into the Command line it works too. It must be a bug?

When you write "run it by the Command line", but exactly did you do? How is it different than typing in at the Command line ?

Ben


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

bpabbott
Administrator
In reply to this post by Tom Bohdan
On Feb 23, 2012, at 6:37 PM, Tom Bohdan wrote:

> --- On Fri, 24/2/12, Ben Abbott <[hidden email]> wrote:
>
>> From: Ben Abbott <[hidden email]>
>> Subject: Re: function help
>> To: "Tom Bohdan" <[hidden email]>
>> Cc: "help-octave Octave" <[hidden email]>
>> Received: Friday, 24 February, 2012, 10:12 AM
>>
>> On Feb 23, 2012, at 5:56 PM, Tom Bohdan wrote:
>>
>> > --- On Fri, 24/2/12, Ben Abbott <[hidden email]> wrote:
>> >
>> >> On Feb 23, 2012, at 4:26 AM, Tom Bohdan <[hidden email]> wrote:
>> >>
>> >>> I have had no success with finding why the following doesn't work. I have tried many examples in Octave's Help and they don't work either.
>> >>>
>> >>> Why is it that if you type each line into the command line then the following works:
>> >>>
>> >>> function total=addr(x,y)
>> >>> total=x+y;
>> >>> endfunction
>> >>>
>> >>> add(5,4)
>> >>>
>> >>> >ans=9
>> >>>
>> >>> but if i put the above lines into a m-file then the interpreter spits out x undefined etc.
>> >>>
>> >>> This happens for all the functions i have found in the LSODE examples etc. If you type them one by one at the command line it works but not in a script file. What am i missing ???
>> >>>
>> >>> regards
>> >>> Tom
>> >>
>> >> Did you intend "add()" or "addr()" ?
>> >>
>> >> Ben
>> >
>> > thanks for your prompt reply.
>> > Another person suggested that it was because there may be a function called add() that may be messing it up but i retried the following and got the same problems:
>> >
>> > function total=exxample(x,y)
>> > total=x+y;
>> > endfunction
>> > exxample(9,8)
>> >
>> > At home i use Ubuntu 10.10 LTS which has octave 2.3.3. I typically use QT octave but i tried it on the command line of Octave itself too.
>> >
>> > At work i use Windows Xp and QT Octave and Octave 2.3.4. Again similar issues BUT i did have some success.
>> >
>> > if i hit Run within the editor of QT octave on the Windows version only the script worked !
>> >
>> > If i run it by the Command line in the dos window it failed exactly like the Linux versions with undefined x in row2, col7 or something like that.
>> >
>> > If its typed line -by-line into the Command line it works too. It must be a bug?
>>
>> When you write "run it by the Command line", but exactly did you do? How is it different than typing in at the Command line ?
>>
>> Ben
>
> by running it at the command line:
>
> (A) I typed each line of the file at the octave prompt and hit enter at the end of each line. After typing in the function, i then wrote exxample(8,9) and hit enter to get ans=17
>
> (B) I saved the function and last line exxample(8,9) into a M-file called exxample.m. i then typed "exxample" at the Octave prompt. It then spits out x undefined, etc

The exxample.m file isn't a script file. It is a function file.

When you enter "exxample", you've call a function, but didn't provide the inputs (x,y). The first time one of the inputs is needed you're get an error "x undefined".

If' you'd like to make exxample.m a script file, the first executable command must be something other that "function ...". For example,

------- begin exxample.m -------
1;
function total=exxample(x,y)
  total=x+y;
endfunction
exxample(9,8)
-------- end exxample.m --------

Ben


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

RE: function help

Damian Harty
In reply to this post by bpabbott
>>> Because by design all the additional function definitions in the .m file
>>> are local and hidden to the outside world.
>>
>> Well, that's the bit I'm not getting. If they are in the script file I'm
>> working in, they don't get much more local than that, do they? I can
>> understand why they would be invisible, say, from the command line despite
>> being present in a script, but I can't understand why they aren't visible
>> when they are /in this file/ and why instead they /have to be in a file with
>> their own name/.
>
> Maybe you can attach the actual script so that others can attempt to duplicate what you are seeing ?

Ah, I did it via that "Nabble" thing and I see it deleted the formatted parts from my earlier post...

The file called add.m contains:

function total=add(x,y)
  total=x+y)
endfunction

The file function_test.m contains:

answer=add(5,9)

This works as advertised.

If at this point I do "which add" it tells me it is "a function from the file add.m" - so far, no surprises.

As I noted earlier, if I now do clear all and which add, there is no existing function. Again, no surprises.

Now, as the original poster noted, if I do clear all, rename add.m to banana.m and then type the following four lines in at the command line (as suggested in the documentation):

function total=add(x,y)
  total=x+y)
endfunction
answer=add(5,9)

...then it also works as advertised. If at this juncture I do "which add" it tells me it is a "command line function".

What doesn't work, and what the original poster found counter-intuitive is that if you put the previous four lines in a file called eric.m then I get an error saying
------------------------------------
Warning: function name `add' does not agree with function file name...
parse error near line 4...

  syntax error

>>>answer=add(5,9)
        ^
------------------------------------

Now, it might well be that this is explained somehow in the help, but as a new starter I am simply commenting that I can't find a succinct explanation and that, moreover, the examples in the documentation give some optimism about grouping functions within a single source file that turns out to be unfounded. I can't comment on whether the aspiration so to do is a good one or a bad one, but I can assert that the confusion on the part of the original poster is entirely understood by me. Both files are .m files, one of them contains a function definition. It isn't stressed that functions need to sit in its own separate file of the same name, and perhaps it could be made clearer to stop people stubbing their toe very early and walking away?



Damian Harty
Senior Research Fellow
Coventry University
+44(0)24 7688 8924
+44(0)7799 414832



_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: function help

damian.harty
In reply to this post by bpabbott
>>> Because by design all the additional function definitions in the .m
>>> file are local and hidden to the outside world.
>>
>> Well, that's the bit I'm not getting. If they are in the script file
>> I'm working in, they don't get much more local than that, do they? I
>> can understand why they would be invisible, say, from the command
>> line despite being present in a script, but I can't understand why
>> they aren't visible when they are /in this file/ and why instead they
>> /have to be in a file with their own name/.
>
> Maybe you can attach the actual script so that others can attempt to duplicate what you are seeing ?

Ah, I did it via that "Nabble" thing and I see it deleted the formatted parts from my earlier post...

The file called add.m contains:

function total=add(x,y)
  total=x+y)
endfunction

The file function_test.m contains:

answer=add(5,9)

This works as advertised.

If at this point I do "which add" it tells me it is "a function from the file add.m" - so far, no surprises.

As I noted earlier, if I now do clear all and which add, there is no existing function. Again, no surprises.

Now, as the original poster noted, if I do clear all, rename add.m to banana.m and then type the following four lines in at the command line (as suggested in the documentation):

function total=add(x,y)
  total=x+y)
endfunction
answer=add(5,9)

...then it also works as advertised. If at this juncture I do "which add" it tells me it is a "command line function".

What doesn't work, and what the original poster found counter-intuitive is that if you put the previous four lines in a file called eric.m then I get an error saying
------------------------------------
Warning: function name `add' does not agree with function file name...
parse error near line 4...

  syntax error

>>>answer=add(5,9)
        ^
------------------------------------

Now, it might well be that this is explained somehow in the help, but as a new starter I am simply commenting that I can't find a succinct explanation and that, moreover, some examples in the documentation give some optimism about grouping functions within a single source file that turns out to be unfounded. I can't comment on whether the aspiration so to do is a good one or a bad one, but I can assert that the confusion on the part of the original poster is entirely understood by me. Both files are .m files, one of them contains a function definition. I read the help documentation on "functions and scripts" and it isn't stressed that each function needs to sit in its own separate file of the same name. The command line behaviour is confusing because there appears to be a "bucket" into which functions can be placed, but this isn't so when running an m-file, as evidenced by my (somewhat laboured) examples above.




Damian Harty
Senior Research Fellow
Coventry University
+44(0)24 7688 8924
+44(0)7799 414832

Senior Research Fellow - Vehicle & System Dynamics Coventry University United Kingdom
Reply | Threaded
Open this post in threaded view
|

Re: function help

damian.harty
In reply to this post by bpabbott
> The exxample.m file isn't a script file. It is a function file.

I read the documentation as pointed out in an earlier post and didn't learn this. I can't comment on whether my reading or the documentation is at fault, or both. What would have taught me is the statement "An m-file that begins with the 'function' is treated as a 'function file' and must have the same name as the function in it. It can only contain one function."

> If you'd like to make a script file, the first executable command must be
> something other than "function ..."

This was in fact the key piece of information that was missing all along. Now everything works as might be expected...

Damian
Senior Research Fellow - Vehicle & System Dynamics Coventry University United Kingdom
Reply | Threaded
Open this post in threaded view
|

Re: function help

Doug Stewart-4
In reply to this post by Damian Harty


On Fri, Feb 24, 2012 at 5:02 AM, Damian Harty <[hidden email]> wrote:
>>> Because by design all the additional function definitions in the .m file
>>> are local and hidden to the outside world.
>>
>> Well, that's the bit I'm not getting. If they are in the script file I'm
>> working in, they don't get much more local than that, do they? I can
>> understand why they would be invisible, say, from the command line despite
>> being present in a script, but I can't understand why they aren't visible
>> when they are /in this file/ and why instead they /have to be in a file with
>> their own name/.
>
> Maybe you can attach the actual script so that others can attempt to duplicate what you are seeing ?

Ah, I did it via that "Nabble" thing and I see it deleted the formatted parts from my earlier post...

The file called add.m contains:

function total=add(x,y)
 total=x+y)
endfunction

The file function_test.m contains:

answer=add(5,9)

This works as advertised.

If at this point I do "which add" it tells me it is "a function from the file add.m" - so far, no surprises.

As I noted earlier, if I now do clear all and which add, there is no existing function. Again, no surprises.

Now, as the original poster noted, if I do clear all, rename add.m to banana.m and then type the following four lines in at the command line (as suggested in the documentation):

function total=add(x,y)
 total=x+y)
endfunction
answer=add(5,9)

...then it also works as advertised. If at this juncture I do "which add" it tells me it is a "command line function".

What doesn't work, and what the original poster found counter-intuitive is that if you put the previous four lines in a file called eric.m then I get an error saying
------------------------------------
Warning: function name `add' does not agree with function file name...
parse error near line 4...

 syntax error

>>>answer=add(5,9)
       ^
------------------------------------

Now, it might well be that this is explained somehow in the help, but as a new starter I am simply commenting that I can't find a succinct explanation and that, moreover, the examples in the documentation give some optimism about grouping functions within a single source file that turns out to be unfounded. I can't comment on whether the aspiration so to do is a good one or a bad one, but I can assert that the confusion on the part of the original poster is entirely understood by me. Both files are .m files, one of them contains a function definition. It isn't stressed that functions need to sit in its own separate file of the same name, and perhaps it could be made clearer to stop people stubbing their toe very early and walking away?



Damian Harty
Senior Research Fellow
Coventry University
<a href="tel:%2B44%280%2924%207688%208924" value="+442476888924">+44(0)24 7688 8924
<a href="tel:%2B44%280%297799%20414832" value="+447799414832">+44(0)7799 414832



_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave

I completely sympathize with both of  you.
And now I would ask for your help.
The Info that you desire ( that would have helped you ) should be in:

Would you take a look at it and suggest improvements to the documentation? 
Would a new sub-chapter be a good place to put it?
I am asking this of you, because it is fresh in your mind, and because you know where you looked (as a beginner), and maybe where it would be found by beginners.

Please help us make this better!!!
I will do the work, just tell me what to do.

Doug Stewart


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
12