How to Find Number of Function Arguments in MATLAB?
Last Updated :
21 Feb, 2022
The number of function arguments passed in MATLAB will be determined in the following article. Unlike C, C++, and Java, MATLAB can accommodate a variable amount of parameters provided into a function without throwing an error. We'll go through how we determine the actual amount of parameters supplied into the function and do the appropriate calculations.
C
#include <stdio.h>
int main()
{
int x = 40;
int y = 50;
printf("Sum of the numbers : %d", GFG(x,y));
int GFG(int a1,int a2, int an)
{
return a1 + a2+ an;
}
}
Output:
too few arguments to function ‘GFG’
Matlab
% MATLAB Code for addition
x = 4;
y = 5;
fprintf("Addition of numbers : %d",GFG(x,y));
function sum = GFG(int a1,int a2..........int an)
sum = a1 + a2 .... + an;
end
Output:
Addition of numbers : 9
nargin():
It returns the number of function input parameters that were provided to the call's currently executing function. nargin() assists you in determining the number of actual input arguments sent into a function so that you can execute the required computation based on those arguments.
Suppose if we want to create a function named 'add_gfg' then the following MATLAB code is used.
Example:
Matlab
% MATLAB code for add
% initialize variables
a = 5; %integer variable a
b = 8; %integer variable b
c = 15; %integer variable c
% here variable a,b,c is passed in
% the function, so sum should be 'a+b+c' i.e 28
fprintf("Addition of three numbers : %d",add_gfg(a,b,c));
% here variable a,b is passed in the function,
% so sum should be 'a+b' i.e 13
fprintf("Addition of two numbers : %d",add_gfg(a,b));
% here variable a is passed in the function,
% so sum should be 'a' i.e 5
fprintf("Addition of one number : %d",add_gfg(a));
% function add_gfg returns the sum
% of the input variables no return function
% is required in case of matlab we initialize the
% variable in which we want to store the final answer and
% return that final answer to the calling
% function, here it is sum
function sum = add_gfg(a,b,c)
cases = nargin; % nargin: returns the actual number
% of input arguments
switch cases
case 1 % if number of input arguments passed is 1,
% return a
sum = a;
case 2 % if number of input arguments passed is 2,
% return sum of 2 numbers
sum = a + b;
case 3 % if number of input arguments passed is 3,
% return sum of 3 numbers
sum = a + b + c;
end
end
Output :

Code Explanation: Above example accepts input parameters and returns the total of the integers sent in. The called function 'add_ gfg' takes input arguments, and nargin returns the total number of input arguments. If the input parameters are three, nargin returns three and stores them in the variable 'cases.' The total of the input arguments is then computed using a switch statement, which is saved in the variable 'sum and returned to the caller function.
nargin(function_name):
This function returns the number of input arguments that appear in the function "function_name". It returns the total length of input arguments that can be passed in the function. Eg. function geeks_for_geeks(int a1, int a2,....int an), the function can take in a total of n arguments and hence "nargin(geeks_for_geeks)" will return 'n' i.e. total n arguments.
Example:
Matlab
% MATLAB code
function_name = 'gfg'; %name of the function
% Following line prints the number of input arguments
% that appear in the function gfg i.e 3
fprintf("\nThe number of arguments appearing in the function gfg
is %d",nargin(function_name));
% Name of the function
function_name1 = 'gfg1';
% Following line prints the number of input
% arguments that appear in the function gfg1 i.e 2
fprintf("\nThe number of arguments appearing in the
function gfg1 is %d",nargin(function_name1));
% The function gfg is not called nor does it print anything
% it is important to create the function or it shows error
% when nargin('gfg') is executed.
function gfg(input1,input2,input3)
%void function
end
% The function gfg1 is not called nor
% does it print anything
% it is important to create the function or it shows
% error when nargin('gfg1') is executed.
function gfg1(input1)
% void function
end
Output :

Code Explanation: In the following example, we have created two functions 'gfg' and 'gfg1' which takes input arguments (input1,input2,input3) and (input1) respectively. So, when we call the command nargin('gfg') and nargin('gfg1'), it returns 3 and 1 as the number of arguments appearing in them are 3 and 1 respectively.
Similar Reads
How to Validate Number of Function Arguments in MATLAB? MATLAB functions are generally defined input and output arguments. To validate the number of these input-output arguments, MATLAB provides us with the following functions: narginchknargoutchkSyntax:narginchk(<minimum-inputs>, <maximum-inputs>) nargoutchk(<minimum-outputs>, <maxi
3 min read
Function With Variable Number of Input Arguments in MATLAB MATLAB functions have the functionality that many other programming languages lack. A MATLAB function can take a variable number of input arguments, without the use of arrays or any additional functionality. The way to go about this in MATLAB is by using the varargin - which can be interpreted as VA
2 min read
How to create a function in MATLAB ? A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read
User defined function in MATLAB Functions let you do a specific task. User defined functions are the functions created by the users according to their needs. This article explains how the user defined function in MATLAB is created. Syntax : function [a1,...,an] = func(x1,...,xm) func is the function name a1,...,an are outputs x1,.
2 min read
Function Argument Validation in MATLAB The basics of Function Argument is, " The functions receive certain inputs from the calling command which applies in function syntax and processes it. sometimes It may or may not produce output argument it dependence completely on the functions efficiency or the accuracy of your code." Function Argu
5 min read