||||
Define a function:
% in_arg stands for input arguments, while out_arg stands for output arguments.
Syntax:
function [out_arg1,out_arg2,...]=function_name(in_arg1,in_arg2,...)
description
end
Define a subfunction (also: local function) into the function (also: main function):
% Only main function can be called from outside (by the Command Window& scripts).
% The input&output arguments within the subfunction is unavailable to the main function and the Command Window. The input&output arguments within the main function is unavailable to the subfunction and the Command Window.
% See e.g. below: we give input arguments to the function myRand, and get a as an output. Then a "value" is copied and passed to M as an input argument of the function sumAllElements, and get summa as an output of sumAllElements. Finally, summa "value" is copied and passed to s as the other output of the function myRand.
% make the function polymorphic by setting variable number of arguments
Two built-in functions:
nargin: returns the number of actual input arguments that the function was called with.
nargout: returns the number of output arguments that the function caller requested.
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-6-2 07:10
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社