1
votes

Just want to confirm if Salesforce Apex don't support parameters like Java and other languages?

For example:

It's a simple function with parameters in Java. And, Salesforce Apex DO NOT support such kind of syntax. Am I right?

public int mult(int x, int y)
{
   return x * y;
}

Salesforce can pass parameters from Javascript or Visualforce Page to Apex, however, I never see parameters within function/class in Apex language so just want to make sure it. If Salesforce have function and parameters then we can write apex code more with efficient structure.

Added by 2012-12-18 ===========================
Thanks Gerard Sexton, it works.

Sample codes:

public class MyClass {
    public integer mult(integer x, integer y)
    {
        return x * y;
    }
}

MyClass m = new MyClass();

System.debug(m.mult(3,4));

The result is 12.

1
It works, thank Gerard Sexton.Cray Kao
Glad that part worked for you. Please mark my answer with the green check. By the way, you dont need to update your answer with code and comments, just the green check is fine.Gerard Sexton

1 Answers

3
votes

Apex does support method parameters.

Here are the docs for defining a class method.

public class MyClass {
    public static integer mult(integer x, integer y)
    {
        return x * y;
    }
}

These methods can be accessed and used ONLY within APEX. To use data passed in from a Visualforce page, please see these docs for an example.

In the visualforce example they use searchText as a class instance property, but in standard programming languages we would pass it in as a method parameter.