Calling a function from another function

2 ビュー (過去 30 日間)
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL 2020 年 11 月 24 日
コメント済み: JITHIN NALUPURAKKAL 2020 年 11 月 24 日
The first function is as below:
function my_first_function1(input,b)
c = input*b;
absx = c;
absx
end
The second function calling the first function is below:
function my_first_subfunction(a)
input = 2;
d = my_first_function1(input,b);
out = d +a;
out
end
Error:
>> my_first_subfunction(a)
Unrecognized function or variable 'b'.
Error in my_first_subfunction (line 3)
d = my_first_function1(input,b);

採用された回答

James Tursa
James Tursa 2020 年 11 月 24 日
my_first_function1( ) doesn't return any value to the caller. To return a value you use this syntax:
function absx = my_first_function1(input,b)
c = input*b;
absx = c;
absx
end
my_first_subfunction( ) uses variable b before it is defined. Examine the code for this function and you will see that there is nothing in the function that defines b before it is used as an input argument to another function.
  6 件のコメント
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL 2020 年 11 月 24 日
function [absx,absy] = my_first_function1(input,b)
c = input*b;
d = inv(c);
absx = c;
absy = d;
absx
absy
end
function my_first_subfunction(input,b,absx,absy)
input = 3;
b = [1 2 4; 3 4 6; 4 2 3];
d = my_first_function1(input,b);
disp('b incomin')
z = absx;
y = absy;
disp(z);
disp(y);
end
>> my_first_subfunction
absx =
3 6 12
9 12 18
12 6 9
absy =
-0.0000 -0.0667 0.1333
-0.5000 0.4333 -0.2000
0.3333 -0.2000 0.0667
b incomin
Not enough input arguments.
Error in my_first_subfunction (line 6)
z = absx;
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL 2020 年 11 月 24 日
Really appreciate your insight.
I tried this way. But even though am passing the return to "my_first_subfunction", its not taking the variables absx and absy from "my_first_function1".

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by