当然.
在这样一个m文件中,本地函数将在主函数后声明.例如:
function y = main_func(x)
% # This is the main function
y = helper_func1(x) .* helper_func2(x); % # Just an example
function h1 = helper_func1(x)
% # This is a helper function #1
h1 = x + 2; % # Just an example
function h2 = helper_func2(x)
% # This is a helper function #2
h2 = x * 2; % # Just an example
在这个例子中,main_func可以没有任何问题地调用helper_func1和helper_func2.你可以测试一下,看看你自己:
>> main_func(8)
ans =
160
没有任何前瞻性声明.
顺便说一下,MATLAB附带的很多m文件都是以这种方式实现的.例如,corrcoef.与corrcoef类型,你可以看到.
注意:在提示符或脚本中不允许使用本地函数定义,因此必须在m文件中声明“main”函数.作为练习,将我的示例复制粘贴到一个新的m文件中,删除main_func的声明(仅第一行),看看会发生什么.