Methods
Summary
This post will cover topics below:
- Definition of method.
- Syntax to define method and invoke it.
- Overload method.
- Exercises.
Definition of a Method
Method
The Group line of codes to perform an operation. It is able to reuse it at different places by invoke it.
Initialise and define a method
Syntax:
- define a method
access_modifier return_value method_name(list of parameters) {
//statement(s) – method body;
}
- invoke a method
method_name (list of parameters);
Type of access_modifier
private
A method is accessible only within its own class. This is default access.
protected
A method is accessible in the inner classes in the defined Apex class and any child classes.
public
A method is accessible by any Apex in the application or namespace.
Global
The method is accessible by any Apex code that has access to the class. Use this access if a method needs to be referenced outside the application.
return_value
Any data type
void: does not have return value
With return value example:
//define a sum() method
public Integer sum(Integer number1, Integer number2) {
return number1 + number2;
}
//print result and invoke sum() method. Result: 12
System.debug('Sum of 2 numbers are: ' +sum(5, 7));
with void example:
//define sum() method
public void sum() {
Integer result = 0; //define a variable and initialise to 0
//loop a number from 1 to 10
for(Integer i = 1; i < 10; i++) {
//if higher than 5, then exit
if(result > 5) {
return;
}
//result: (0, 1, 3 )
System.debug('result: ' +result);
result +=i; //increment by 1
}
System.debug('Hello World'); //does not get executed
}
//invoke sum()
sum(); //result: (0, 1, 3)
Info
break
It exits loop iteration when criteria has fulfill
return
It exits loop iteration and method.
From the example above, using return, the Hello World does not get executed
"Hello World" gets execute if using break keyword
Overload Method
A method with same name but with different parameter lists.
Example
//declare Integer max() method
public Integer max(Integer num1, Integer num2) {
//if num1 > num2, return num1
return num1 > num2 ? num1 : num2;
}
//declare Decimal max() method
public Decimal max(Decimal num1, Decimal num2) {
//if num1> num2. return num1
return num1 > num2 ? num1 : num2;
}
System.debug('Max result ' +max(2, 3)); //res: 3
System.debug('Max result ' +max(2.5, 3.3)); //res: 3.3
Exercises
1. Write a method to display 3 numbers in increasing order.
- public void sortedNumber (Integer num1, Integer num2, Integer num3)
2. Write a method that computes future investment value at a given interest rate for number of years. Displays the result in the table that displays future value for the yeas from 1 to 30.
For example:
For example:
- Amount of money: 1000
- Interest rate: 2.5 % annually
- total years: 30 years
Formula:
monthlyInterestRate = annualInterestRate / 1200;
monthlyPayment = loanAmount * monthlyInterestate / (1 – 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
totalPayment = monthlyPayment * numberOfYEars * 12;
3. (Summing all elements) Write a method with one parameter. Use that parameter to loop through increment number from 0 to parameter_val and sum all of them.
Example inside the body: for(Integer i = 0; i < parameter_list; i++)
4. (Finding the largest element) Write a method with one parameter. Use that parameter to loop through increment number from 0 to parameter_val and display the largest number on the screen