Matlab/Octave 函数体中是否有分号?
semicolons or not in Matlab / Octave function bodies?
在 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
在 Matlab/Octave 代码行的末尾放置一个分号可防止控制台打印 answer/variable assignment/whatever.
x=5
将 x 设置为 5 并打印 x 的值。
x=5;
将 x 设置为 5。
在 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
在 Matlab/Octave 代码行的末尾放置一个分号可防止控制台打印 answer/variable assignment/whatever.
x=5
将 x 设置为 5 并打印 x 的值。
x=5;
将 x 设置为 5。