Not enough input arguments
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, I wrote a function that needs to accept user input and then call the function ussing that input. However, it says not enough input arguments.
%% Section 1: Initalization of Variables
%[r, h] = cylind(r, h);%Intializing the varibles
r = input('What is the r?');
h = input('What is the h?');
%% Section 2: Result Output
fprintf("v is %d, sa is %d\n", cylind)
%% Section 3: Processing
cylind(r, h)
function [r, h] = cylind(r, h) %function for surface area and volume
v = pi*(r^2)*h;
sa = (2*pi*(r^2)) + (2*pi*r*h);
end
0 Commenti
Risposte (2)
Stephen23
il 13 Ott 2020
Modificato: Stephen23
il 13 Ott 2020
There are two main bugs that we need to fix:
- define the correct function outputs.
- call the function with input and output arguments.
Lets have a look at them in detail.
1- Currently you define the function with exactly the same input and output arguments:
function [r, h] = cylind(r, h)
While this is perfectly valid and useful in some situations, in your case you want to return the values of the internal calculations, i.e. the variables v and sa, so these need to be defined as the output arguments, e.g. like this:
function [v, sa] = cylind(r, h)
2- You call the cylind function twice, but the first time with no input arguments (even though it requires two input arguments) and the secdon time with no output arguments. Calling a function with no output arguments is perfectly valid, but it just means that any outputs it calculates internally are simply discarded. If you want to use the outputs then you need to assign then to some variables, e.g.:
[Vout,SAout] = cylind(..)
Note that the names used internally are totally unrelated to the names used in the calling workspace!
So we can call the fixed function:
function [v,sa] = cylind(r,h)
v = pi*(r.^2).*h;
sa = (2*pi*(r.^2))+(2*pi*r.*h);
end
simply like this:
>> Rin = 2;
>> Hin = 3;
>> [Vout,SAout] = cylind(Rin,Hin)
Vout = 37.699
SAout = 62.832
̀>> fprintf("v is %g, sa is %g\n", Vout,SAout) % changed to %g
v is 37.6991, sa is 62.8319
0 Commenti
Vedere anche
Categorie
Scopri di più su Performance and Memory in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!