Calling a function from another function

2 views (last 30 days)
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);

Accepted Answer

James Tursa
James Tursa on 24 Nov 2020
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 Comments
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL on 24 Nov 2020
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 on 24 Nov 2020
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".

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!