简体   繁体   English

可以在shebang可执行GNU Octave文件中定义函数吗?

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

Given an Octave m file heading with #! /usr/bin/octave -q 给定一个以#! /usr/bin/octave -q为首的Octave m文件#! /usr/bin/octave -q #! /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? #! /usr/bin/octave -q ,可以在此文件内定义函数吗?或者唯一的方法是调用另一个文件中定义的函数?

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; 唯一的问题是,第一个Octave语句不能为函数定义,这就是为什么许多Octave程序将以1;开头的原因1; . However, my experience is that most Octave programs need a package so the first statements can be just the loading of said packages. 但是,我的经验是,大多数Octave程序都需要一个程序包,因此第一个语句可以只是加载所述程序包。

Here's an example Octave program: 这是一个Octave程序示例:

#!/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 ());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM