Variadic Functions in Wolfram Language

Here’s a function that will take an arbitrary number of ints as arguments.

sum[nums__Integer] := Module[{total = 0},
   Print[{nums}, " "];
   total = Plus @@ {nums};
   Print[total];
];

(* 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 list, apply them to a variadic function using Apply (`@@`). *)
numList = {1, 2, 3, 4};
sum @@ numList

To execute the Wolfram Language code, you can use a Wolfram Language environment such as Mathematica, Wolfram Script, or the Wolfram Cloud. Here, we define a function sum that takes an arbitrary number of integer arguments using the pattern nums__Integer. The function prints the list of arguments and their sum. You can call this function with individual arguments or a list of arguments using the Apply function (@@).