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
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
a = 5;
b = 8;
c = 15;
fprintf( "Addition of three numbers : %d" ,add_gfg(a,b,c));
fprintf( "Addition of two numbers : %d" ,add_gfg(a,b));
fprintf( "Addition of one number : %d" ,add_gfg(a));
function sum = add_gfg(a,b,c)
cases = nargin;
switch cases
case 1
sum = a;
case 2
sum = a + b;
case 3
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
function_name = 'gfg' ;
fprintf("\nThe number of arguments appearing in the function gfg
is
function_name1 = 'gfg1' ;
fprintf("\nThe number of arguments appearing in the
function gfg1 is
function gfg(input1,input2,input3)
end
function gfg1(input1)
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...