MATLAB: How can i fix this error?why is the function unused even though its written in the same manner as the preceding function that work

nestedfunctionunusedvariable

function [V,S] = prism (H,W,L) %primary function
h=H;
w=W;
l=L;
V =vlm; h;w;l;
S = sfa; h;w;l;
%Prm =prm; w;l;
function V = vlm; h;w;l; %subfunction for calculating the volume
V = (h*w*l);
function S= sfa; h;w;l; %subfunction for calculating the surface area
S = 2*(h*l+h*w+w*l);
% function Prm = prm, w;l; %subfunction for calculating the perimeter
% Prm = (2*w+l*2);
% end
end
end
end

Best Answer

Shane - what is the error you are observing? If I run your code (with random inputs to prism), I see
Undefined function or variable 'sfa'.
Error in prism (line 6)
S = sfa; h;w;l;
If sfa is a function to calculate the surface area, then are you intending to pass h, w, and l as input parameters to this function? If so, the code would be
S = sfa(h,w,1);
This assumes that the function signature and body for sfa are adefined as
function S = sfa(h,w,l)
S = 2*(h*l+h*w+w*l);
end
Note how the signature includes the input parameters which you then must pass into this function when calling it. See Declare function name, inputs, and outputs for details on how to correctly define your function.
If you are trying to nest your functions (see Nested Functions for details), then you don't have to pass the height, width, and length variables that are declared in the parent function because they would be visible to the nested (child) function. The same would be true for the input parameters to prism and so you do not have to declare the local h, w, and l and your code could simply be
function [V,S] = prism (H,W,L)
V =vlm;
S = sfa;
function [V] = vlm
V = (H*W*L);
end
function [S]= sfa
S = 2*(H*L+H*W+W*L);
end
end
See how the end pairs with each function declaration - this may be a source of some of your errors/bugs as your nested functions don't seem to have this same pairing. sfa is nested within vlm which is nested within prism.