简体   繁体   中英

Undefined argument when declaring function in Octave

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

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

You can't start a script with a function keyword. https://www.gnu.org/software/octave/doc/v4.0.1/Script-Files.html

This 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 get:

warning: function 'myrand' defined within script file 'myrand.m'   
Running ...  
a =  91  
big_rand =  489489  
result =  48.949  
mrand =  48.949  
function wakeup(message)
  printf("\a%s\n",message)
endfunction 


wakeup("Rise and shine!");

So this was my function and after trying multiple times, I noticed that the function was not called with an argument, so the terminal kept throwing an undefined error.

Eg

function was called like this "wakeup", instead of like this "wakeup("Rise and shine!")"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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