call function with fewer parameters

42 views (last 30 days)
aldo
aldo on 24 Oct 2023
Answered: Voss on 24 Oct 2023
gg(a)
function gg(a,b,c)
end
what happen if i call function with 1 parameter instead of 3
  1 Comment
Torsten
Torsten on 24 Oct 2023
Edited: Torsten on 24 Oct 2023
No need to explain this behaviour, I guess.
a = 2;
gg(a)
ans = 4
gg_error(a)
Not enough input arguments.

Error in solution>gg_error (line 10)
result = a.^2 + b.^2;
function result = gg(a,b,c)
result = a.^2;
end
function result = gg_error(a,b,c)
result = a.^2 + b.^2;
end

Sign in to comment.

Accepted Answer

Voss
Voss on 24 Oct 2023
"what happen if i call function with 1 parameter instead of 3"
The answer depends on what the function does with the parameters.
I've defined two functions below: gg_1, which takes three inputs and uses the first one only, and gg_all, which takes three inputs and uses them all. Whether I call gg_1 with one input or two or three, it runs without error, but when I call gg_all with fewer than three inputs, an error occurs at the first line of code that tries to access an input that was not given. See below.
You can use nargin to determine how many inputs were given (and write code to handle different possible number of inputs).
a = 1;
b = 2;
c = 3;
gg_1(a,b,c) % three inputs given; only the first is used; ok
1
gg_1(a) % one input given, which is used; ok
1
gg_all(a,b,c) % three inputs given; all are used; ok
1 2 3
gg_all(a) % one input given; expected three; error
1
Not enough input arguments.

Error in solution>gg_all (line 20)
disp(b); % error here if b is not given
% this function uses the first parameter only
function gg_1(a,b,c)
disp(a);
end
% this function uses all three parameters
function gg_all(a,b,c)
disp(a); % error would happen here if a were not given
disp(b); % error happens here when a is given but b is not given
disp(c); % error would happen here if a and b were given but not c
end

More Answers (1)

Walter Roberson
Walter Roberson on 24 Oct 2023
In MATLAB, it is not directly an error to call a function with fewer parameters than it is defined with.
Instead, at the time that you try to use one of the names defined as a parameter, if fewer parameters have been passed and you did not already assign to that variable, then you will get an error about not enough parameters.
aa = 123;
gg(aa)
a = 123
b = 456
c = 789
Not enough input arguments.

Error in solution>gg (line 11)
d
function gg(a,b,c,d)
if nargin < 2
b = 456;
end
c = 789;
a
b
c
d
end
Here we do not pass in a value for b, but that is okay because we used nargin to detect the situation and assigned a default value for b.
Here we do not pass in a value for c, but that is okay because we ignored whatever c was passed in and always assigned a value to c before we tried to use it.
Here we do not pass in a value for d, and by the time we need d, we still have not assigned a value to d, so it is an error.

Community Treasure Hunt

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

Start Hunting!