Calling a function from another function

2 visualizaciones (últimos 30 días)
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL el 24 de Nov. de 2020
Comentada: JITHIN NALUPURAKKAL el 24 de Nov. de 2020
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);

Respuesta aceptada

James Tursa
James Tursa el 24 de Nov. de 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 comentarios
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL el 24 de Nov. de 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 el 24 de Nov. de 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".

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by