Not enough input arguments

조회 수: 1 (최근 30일)
Serena Simpson
Serena Simpson 2020년 10월 13일
편집: Stephen23 2020년 10월 13일
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

답변 (2개)

Bandar
Bandar 2020년 10월 13일
편집: Bandar 2020년 10월 13일
You are defining the names of inputs like the names of the ouputs. The proper header of function is like this
function [outputs] = <function_name>(inputs)

Stephen23
Stephen23 2020년 10월 13일
편집: Stephen23 2020년 10월 13일
There are two main bugs that we need to fix:
  1. define the correct function outputs.
  2. 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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by