Error: function undefined in script file

Hi. I have just started using Octave and cannot am cannot get my script files to recognize functions defined in the script files. For example, given the following script file:

 a = 3
 b = Adder(a)

 function retval = Adder (input1)
 retval = input1 + 5
 endfunction

When I run the script file, the following appears in the Command Window:

 >> test5

 a =  3
 error: 'Adder' undefined near line 2 column 5
 error: called from
     test5 at line 2 column 3
 >> 

If I put the function in a separate file then the script works fine.

I am using:

Any help with this issue would be greatly appreciated.

In this language you must define the function before you call (use) it.

 a = 3

 function retval = Adder (input1)
 retval = input1 + 5
 endfunction

 b = Adder(a)

Okay. I didn’t realize that. Thank-you.

it appears that in 2016 Matlab added the ability to have Local functions in script files that can be appended to the end of the file as you did above. There has been a good amount of discussion about this on the maintainers list, but so far because of the amount of work required to make the change Octave has not yet implemented a compatible version of this yet. So, for now, local functions have to appear as Doug described. Alternatively, you can have functions in their own m-files. they can then be called by any script

nrjank,

Thanks for the background information. I can imagine that making the required changes would be a lot of work.

Regards

Has this required order of definition been changed lately? I ran into the same issue, but found an example in which the function is defined after it is called and the example does work (Octave 4.6.0 on Windows 10):

# not a function file:
disp(1);

ii = 10;
disp(ii)
ii = do_something ();
disp(ii)

function ii= do_something ()
  ii=0;
endfunction

Hi Dennis_Zeilstra,
I tried the example you posted and got “error: ‘do_something’ undefined”. I note that whereas you used v4.6.0, I used was v6.2.0. That said, I don’t know how to explain that the posted example worked for you.

Regards

Oh, my mistake. I am using v6.4.0 not 4.6.0 (typo). Nevertheless, I agree that this example does seem to defy the way Octave handles function definitions in script files.

There has been no change in the way functions in script files work in Octave. But if your script was written for Matlab, then the rules are different and local functions must appear at the end of the script. That feature was added to Matlab in 2016. Octave has always allowed functions in script files, but they must be defined before they are used. This difference is a known compatibility issue.

Dear John (@jwe), I know, but the example that I provide above (defining a function at the end of the file) does run in Octave. Can you explain why that could be the case?

do you clear state between script runs (“clear all”) or similar? if you don’t clear the state, then it might remember the function definition from a prior run.

It doesn’t work for me. Saving the script

# not a function file:
disp(1);

ii = 10;
disp(ii)
ii = do_something ();
disp(ii)

function ii= do_something ()
  ii=0;
endfunction

in foo.m, I see:

octave:1> foo
1
10
error: 'do_something' undefined near line 6, column 6
error: called from
    foo at line 6 column 4
octave:2> version
ans = 6.4.0

so I’m guessing you must have defined do_something in some other way prior to calling the script?

Mmm. I think @nrjank is right. When I run the script after a clean start, it doesn’t work. This solves it: apparently I hadn’t cleared all variables and functions from the memory when I tried it before. Conclusion: Octave still works as we know it :slight_smile:. Thank you all.