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.
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