フィルターのクリア

What would be the output of the below code snippet and why ?

4 ビュー (過去 30 日間)
Ali Q
Ali Q 2021 年 1 月 8 日
回答済み: Swaroop Gopa Srinivas 2021 年 2 月 24 日
var x = 0;
a();
function a() {
var x = 2;
b();
console.log(x);
}
function b() {
x++;
console.log(x);
}

回答 (1 件)

Swaroop Gopa Srinivas
Swaroop Gopa Srinivas 2021 年 2 月 24 日
I assume that output of below Matlab code is required with reason.
x = 0;
a();
function a()
x = 2;
b();
disp(x);
end
function b()
x=2;
x = x+1;
disp(x);
end
(console.log(), var and ++ are not a valid Matlab commands)
>>MLAnswer
3
2
Reason:
x is a local variable and the scope of this variable is within the function only.
Among the display functions: disp(x) inside function b() is executed 1st, where the value of x is 3 (x=2; x=x+1). Then disp(x) inside function a() is executed, where the value of x is 2 (x=2)

カテゴリ

Help Center および File ExchangeDeployment, Integration, and Supported Hardware についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by