Calling a function from another function

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);

 Risposta accettata

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 Commenti

Thanks for the answer. But b has already been defined in my workspace as
b =
-0.5000 0.2500 0.2500
-3.0000 2.5000 -0.5000
2.5000 -1.7500 0.2500
It may exist in the base workspace, but it does not exist in the workspace of your my_first_subfunction function. See this documentation page for a brief description of the difference.
Thanks. But the first function had used the base workspace while executing the calculation. i.e while calling the first function i used my_first_function1(2,b). So 'input' being 2 and 'b' was extracted from the base workspace.
So 'input' being 2 and 'b' was extracted from the base workspace.
No.
The main way variables get from the base workspace to a function workspace is by being passed into the function when it is called from the base workspace. If one function B is being called from another function A then A can pass variables from its workspace into B by specifying them as input arguments. But neither A's workspace nor B's workspace automatically has access to the base workspace. [While there are ways for them to access the base workspace, generally speaking we discourage the use of the functions that you would need to explicitly use to do that.]
If you want your function B called from function A to have access to data from the base workspace, you will need to pass it into A's workspace as an input argument so A has access to it and can pass it into B.
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;
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".

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by