octave:1> function wakeup > printf ("hello function\n"); > endfunction octave:2> wakeup() hello function
# 带参数的函数 octave:3> function wakeup (message) > printf ("this is my args: %s\n", message); > endfunction octave:4> what M-files in directory /Users/zchanglin/Desktop/OctaveWK:
draw_sin_cos.m ustep.m octave:5> wakeup('hello function with args') this is my args: hello function with args octave:6>
带返回值的函数
带参数的函数和不带参数的有返回值函数通常遵循如下格式:
1 2 3 4 5 6 7
function ret-var = name (arg-list) # 单个返回值 body endfunction
function [ret1, ret2, ret3] = name (arg-list) # 多个返回值 body endfunction
来看看具体的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
octave:1> function retval = add (a, b) > retval = a + b; > endfunction octave:2> add(10,20) ans = 30 octave:3> function [ret1,ret2,ret3] = allAddOne(a, b, c) > ret1 = a + 1; > ret2 = b + 1; > ret3 = c + 1; > endfunction; octave:4> [r1,r2,r3] = allAddOne(10, 20, 30); octave:5> r1 r1 = 11 octave:6> r2 r2 = 21 octave:7> r3 r3 = 31
在 Octave 中,三角函数都是使用的弧度制,但是有时候使用角度制也比较的方便。但是使用角度制的时候每次都需要使用 $sin(d/180*pi) $将角度 d 转换为弧度制并调用三角函数。但是若是使用一个函数 $sind(d)$ 会让代码更加的简单异读。那么可以创建这么一个函数,创建一个名为sind.m 的文本文件并包含以下内容:
1 2 3 4
function s=sind(x) % SIND(x) Calculates sine(x) in degrees s=sin(x*pi/180); endfunction
这个函数看似简单,但是很多函数都是这样简单但是很有效。同样的,对于任意一个函数,都可以使用 help 命令阅读关于此函数的说明:
octave:1> function s=sind(x) % SIND(x) Calculates sine(x) in degrees s=sin(x*pi/180); endfunction octave:2> help sind 'sind' is a command-line function
SIND(x) Calculates sine(x) in degrees
Additional help for built-in functions and operators is available in the online version of the manual. Use the command 'doc <topic>' to search the manual index.
Help and information about Octave is also available on the WWW at https://www.octave.org and via the help@octave.org mailing list. octave:3> sind(30) ans = 0.5000 octave:4> sind(90) ans = 1 octave:5> sind([30 60 90]) ans =
octave:1> function varargout = one_to_n () for i = 1:nargout varargout{i} = i; endfor endfunction octave:2> [a, b, c, d] = one_to_n() a = 1 b = 2 c = 3 d = 4 octave:3>
octave:1> function ret = add(x) % Compute the sum of all the elements of the vector tmp = 0; m = length(x); if(m < 2) error("args nums error, must >= 2") endif for n=1:m tmp = tmp + x(n); endfor ret = tmp; endfunction octave:2> add([0]) error: args nums error, must >= 2 error: called from add at line 5 column 9 octave:3> add([0 3 4 5]) ans = 12
octave:1> function print_arguments (varargin) narginchk(1, 3) for i = 1:length (varargin) printf ("Input argument %d: ", i); disp (varargin{i}); endfor endfunction octave:2> print_arguments(2,3,4,5) error: narginchk: too many input arguments error: called from narginchk at line 60 column 5 print_arguments at line 2 column 1 octave:3> print_arguments(2,3) Input argument 1: 2 Input argument 2: 3 octave:7>
校验返回值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
octave:1> function varargout = one_to_n () nargoutchk(1, 3) for i = 1:nargout varargout{i} = i; endfor endfunction octave:2> [a b c d] = one_to_n() error: nargoutchk: Too many output arguments. error: called from nargoutchk at line 102 column 7 one_to_n at line 2 column 3 octave:3> [a, b, c] = one_to_n() a = 1 b = 2 c = 3 octave:4>