nightcod3r
nightcod3r

Reputation: 792

Can functions be defined within a shebang executable GNU Octave file?

Given an Octave m file heading with #! /usr/bin/octave -q, can a function be defined inside this file?, or the only way to do it is to invoke functions defined in another file?

Upvotes: 0

Views: 209

Answers (1)

carandraug
carandraug

Reputation: 13081

Yes, they can. The only thing is that the first Octave statement must not be a function definition which is why many Octave programs will start with 1;. However, my experience is that most Octave programs need a package so the first statements can be just the loading of said packages.

Here's an example Octave program:

#!/usr/bin/env octave
## Do not forget your license

pkg load foo;
pkg load bar;

1; # not really necessary because of the pkg load statements above

function foobar ()
  ## this function does something amazing
endfunction

function main (argv)
  disp (argv);
endfunction

main (argv ());

Upvotes: 4

Related Questions