Matlab / Octave函数体中是否有分号?

时间:2015-02-06 20:14:46

标签: matlab octave

考虑Octave / Matlab中的以下两个函数;他们之间的唯一区别是身体中的线条是否以分号结束:

function [a, b, c] = fooNoSemicolons (x, y, z)
  a = x * 42
  b = y + 42
  c = (x * y) / (z * 42)
endfunction

function [a, b, c] = fooSemicolons (x, y, z)
  a = x * 42;
  b = y + 42;
  c = (x * y) / (z * 42);
endfunction

现在考虑以下调用及其输出:

fprintf ("no semicolons: \n"); disp (fooNoSemicolons (12, 13, 14));
fprintf ("done\n");
fprintf ("semicolons:    \n"); disp (fooSemicolons (12, 13, 14));
fprintf ("done\n");
no semicolons:
a =  504
b =  55
c =  0.26531
 504
done
semicolons:
 504
done

显然,两个函数都产生相同的答案,即a的值,即504,但我不太确定内部是否存在语义上的差异。

我无法在Matlab / Octave中找到有关分号含义的文档,但我做了一些实验并没有太多亮点:

fprintf ("noSemicolons: \n");
[someA, someB, someC] = fooNoSemicolons (12, 13, 14);
fprintf ("done\n"); 
disp ([someA, someB, someC]);

fprintf ("semicolons:\n");
[moreA, moreB, moreC] = fooSemicolons (12, 13, 14);
fprintf ("done\n");
disp ([moreA, moreB, moreC]);

fprintf ("noSemicolons: \n");
otherStuff = fooNoSemicolons (12, 13, 14);
% otherStuff apparently does not get bound to an array or vector!
disp (otherStuff);
fprintf ("done\n"); 

fprintf ("semicolons:\n");
moreStuff  = fooSemicolons (12, 13, 14);
% moreStuff apparently does not get bound to an array or vector!
fprintf ("done\n"); 
disp (moreStuff);
noSemicolons:
a =  504
b =  55
c =  0.26531
done
   504.00000    55.00000     0.26531
semicolons:
done
   504.00000    55.00000     0.26531
noSemicolons:
a =  504
b =  55
c =  0.26531
 504
done
semicolons:
done
 504

1 个答案:

答案 0 :(得分:7)

在Matlab / Octave代码行的末尾加一个分号可防止控制台打印答案/变量赋值/无论如何。

x=5

将x设置为5并打印x的值。

x=5;

将x设置为5。