function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Functions with varying number of parameters

Is there a way to create a function that can take in a varying number of parameters? 


I know in java to do such a thing you can have a function like this: 


public double average(double...values) { 

 ....function body....



Is there a simliar method to do this in APEX? 


Thanks in advance!
List<String>, List<Object>?

What do you mean by varying number of parameter? Please see below if it solves your issue:


You can create a method with all the parameters that you have to pass like:


public void genMethod(Var a, Var b, Var c, Var d)


   //your code here



Now when you are calling from different places you can pass the parameters that are required and all the other parameters as NULL. 


className.genMethod(null, b, null, null);




className.genMethod(a, null, null, d);





Apex doesn't support variable number of parameters. If your requirement is to call the method with variable number of parameters, try using a map (List's will take only same type of object for all it's values - maps can take different type of objects in a same variable). For e.g.


public void myMethod(Map<String, Object> mapParams) {

// string is the key and the object can be of any object including sObjects. You need to type case in order to work.






Create a parameter class and use that as a parameter:


public class AvarageParameter {


   //all the varying number of params




public boolean average( AverageParameter param) {




The benefit of doing this with a parameter class is that you can change the parameters in the class, causing a smaller impact on exising code than changing the function parameters.