Difference Between Method and Function in Salesforce Apex 

Difference Between Method and Function in Salesforce Apex

What is a Method in Apex?

A method in Apex is a block of code that performs a specific task and is always defined inside a class. It can take input parameters, process them, and optionally return a value. Apex methods are always associated with a class, whether it’s a built-in system class or a custom class. 

Examples of Built-in Classes and Methods in Apex

Class Method
Math Math.max()
Math Math.min()
String String.valueOf()
DateTime DateTime.now()
System System.debug()
Decimal Decimal.valueOf()

Example of a Method in Apex

Scenario: Create a custom function to find the maximum number without using Math.max(). 

apex 

public class DemoMethod { 

    public static void findMaximum() { 

        Integer a = 40; 

        Integer b = 60; 

        Integer result = Math.max(a, b); 

        System.debug(‘Maximum number is: ‘ + result); 

} 

} 

Explanation

•  DemoFunction is a custom class

 findMaximum() is a custom function method created to compare two numbers

  Math.max(a, b) is a built-in method from the Math class that returns the larger of two numbers.
  System.debug() prints the result to the console (used in Apex to output logs).

What is a Function in Apex?

A function in Apex is technically just a method that performs a specific job and is often custom-built by developers. It can also return a value and take parameters. Apex does not have a separate “function” keyword like some languages (e.g., JavaScript or Python), so functions are written as methods inside classes. 

 So in Apex, functions = custom methods that perform specific tasks. 

Example of a Function in Apex

Scenario: Create a custom function to find the maximum number without using Math.max(). 

apex 

public class DemoFunction { 

    public static void findMaximum() { 

        Integer num1 = 10; 

        Integer num2 = 20; 

        Integer result = (num1 > num2) ? num1 : num2; 

        System.debug(‘Maximum number is: ‘ + result); 

    } 

} 

Explanation

• DemoFunction is a custom class.
• findMaximum() is a custom function (method) created to compare two numbers.
• The ternary operator checks which number is larger and assigns it to result.
• System.debug() prints the final result, which is 20.

Practice Tasks

Try creating these in your Developer Console or Salesforce Org:
        1. Use a built-in method Math.min() to find the smaller number between 20 and 30.
        2. Create a custom function to find the sum of two numbers.
        3. Use String.valueOf() to convert a number to a string.
        4. Create a custom function that checks if a number is even or odd.
        5. Use DateTime.now() to display the current date and time.
        6. Create a function to return the maximum of three numbers.
        7. Use System.debug() to print the result of Decimal.valueOf(‘99.99’).
        8. Call a method from a different class using ClassName.MethodName();.

Course Video