Undefined argument when declaring a function in Octave

I get a variable / argument undefined when I try to define my own random generator function.

the code:

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)

Error:

>> myrand
error: 't' undefined near line 2 column 15
error: called from
myrand at line 2 column 7
+6
source share
1 answer

You cannot run a script with a function keyword. https://www.gnu.org/software/octave/doc/v4.0.1/Script-Files.html

It works:

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) 

You should receive:

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

Source: https://habr.com/ru/post/1657044/


All Articles