Title here
Summary here
Here’s a function that will take an arbitrary number of int
s as arguments.
function sum(varargin)
disp(varargin + " ")
total = 0;
for i = 1:length(varargin)
total = total + varargin{i};
end
disp(total)
endfunction
Within the function, the type of varargin
is equivalent to a cell array. We can call length(varargin)
, iterate over it with for
loop, etc.
function main()
// Variadic functions can be called in the usual way with individual arguments.
sum(1, 2)
sum(1, 2, 3)
// If you already have multiple args in a matrix, apply them to a variadic function using varargin like this.
nums = [1, 2, 3, 4]
sum(nums{:})
endfunction
Another key aspect of functions in Scilab is their ability to handle cell arrays and variadic arguments, which we’ll look at next.
main()
To run the program, save the code into a .sce
file and execute it in the Scilab console.
--> exec('filename.sce')
[1 2]
3
[1 2 3]
6
[1 2 3 4]
10
Now that we can run and handle variadic functions in Scilab, let’s learn more about the language.