在Octave中声明函数时未定义的参数

时间:2016-10-07 10:27:39

标签: function octave

在尝试定义我自己的随机生成器函数时,我得到未定义的变量/参数。

代码:

function result = myrand(n, t, p, d)
    a = 200 * t + p
    big_rand = a * n
    result = big_rand / 10**d
    return;
endfunction

mrand = myrand(5379, 0, 91, 4)

错误:

>> myrand
error: 't' undefined near line 2 column 15
error: called from
myrand at line 2 column 7

2 个答案:

答案 0 :(得分:10)

您无法使用 function 关键字启动脚本。 https://www.gnu.org/software/octave/doc/v4.0.1/Script-Files.html

这有效:

disp("Running...")
function result = myrand(n, t, p, d)
     a = 200 * t + p
     big_rand = a * n
     result = big_rand / 10**d
     return;
endfunction

mrand = myrand(5379, 0, 91, 4) 

你应该得到:

warning: function 'myrand' defined within script file 'myrand.m'   
Running ...  
a =  91  
big_rand =  489489  
result =  48.949  
mrand =  48.949  

答案 1 :(得分:-1)

function wakeup(message)
  printf("\a%s\n",message)
endfunction 


wakeup("Rise and shine!");

这是我的函数,多次尝试后,我注意到该函数没有使用参数调用,因此终端不断抛出未定义的错误。

例如

<块引用>

函数被称为“唤醒”,而不是像这样 "wakeup("崛起和闪耀!")"