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.

How to Find Number of Function Arguments in MATLAB?

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

Similar Reads

nargin():

...

nargin(function_name):

...