ඉතින් යාලුවනේ ඔන්න අදත් මම ඔයාලට තවත් අලුත් පාඩමක් අරගෙන ආවා. ටිකක් පරක්කු උනාට sorry වෙන්න ඕනේ මේ දවස් වල අම්බානකට වැඩ වලට හිරවෙලා ඉන්නේ හික් හික්.... ;)
ඉතින් අද මම කියල දෙන්න යන්නේ java වල අපිට හම්බවෙන Methods ගැන පොඩි විස්තරයක් ඔයාලට මතක ඇති කලින් පාඩම් වලදී අපි කරපු access modifiers අපිට ඒවා මෙතැනදී ඕනේ වෙනවා. methods වලටම අපි කියනවා Procedures or Functions කියලත් Procedures කියන්නේ value එකක් return කරන්නේ නෑ Functions කියල කියන්නේ අපි value එකක් return කරන ඒවාට,
අපි එහෙනම් බලමු කොහොමද method එකක් create කරගන්නේ කියල
බලන්නකෝ syntax එක එහෙනම්,
modifier returnType nameOfMethod (Parameter List) {
// method body
}
- modifier: It defines the access type of the method and it is optional to use.
- returnType: Method may return a value.
- nameOfMethod: This is the method name. The method signature consists of the method name and the parameter list.
- Parameter List: The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
- method body: The method body defines what the method does with statements.
මම ඔයාලට මේ syntax එකේ විදියට හදපු මේ method එක විස්තර කරලා දෙන්නම් කෝ
public static int funcTest(int a, int b){
//Method Body
}
බලන්නකෝ මේ method එකේ
- public static : modifier
- int : return type
- funcTest : function name
- a, b : formal parameters
- int a, int b : list of parameters
NOTE:- මතක තියාගන්න method එකක name එක ලියනකොට ලියන්න ඕනේ පලවෙනි word එකේ පලවෙනි අකුර simple දෙවැනි word එකේ ඉදන් capital වලින්.
අපි දැන් එහෙනම් බලමු කොහොමද method එකක් call කරන්නේ කියල එහෙනම් හදාගතට වැඩක් න නේ call කරගන්න බැරි නම් :P හික් හික්.....
method එකක් call වෙන විදි දෙකක් තියනවා එකක් තමයි
- method returns a value
- returning nothing (no return value)
method එකක් call කරහම වෙන දේවල් දෙකක් තියනවා ඒ තමයි
වැඩේ ගොඩ නේද එහෙනම් එහෙනම් අපි දැන් උදාහරණයක් කරලා බලමු.
- return statement is executed.
- reaches the method ending closing brace.
public class ExampleMaxNumber{
public static void main(String[] args) {
int a = 50;
int b = 25;
int c = maxFunction(a, b);
System.out.println("Max Value = " + c);
}
public static int maxFunction(int n1, int n2) {
int min;
if (n1 < n2)
min = n2;
else
min = n1;
return min;
}
}
ඔයාලට මේකේ output එක එන්න ඕනේ
Max Value = 50 කියල.
කොහොමද එළකිරි වගේ වැඩ නේද ;)
ඊටපස්සේ මම ඔයාලට කියල දෙන්න යන්නේ void keyword එක ගැන පොඩි විස්තරයක් මේ keyword එක use කරහම වෙන්නේ method එකෙන් value එකක් return වෙන්නේ නෑ.
බලන්නකෝ එහෙනම් මේ example එක
public class ExampleVoid {
public static void main(String[] args) {
methodRankPoints(155.5);
}
public static void methodRankPoints(double points) {
if (points >= 100.2) {
System.out.println("Rank:A1");
} else if (points >= 50.3) {
System.out.println("Rank:A2");
} else {
System.out.println("Rank:A3");
}
}
}
මේකේ result එක විදියට එන්න ඕනේ
Rank:A1 කොහොමද වැඩේ එළකිරි එකට වැඩ නේද හික් හික්...... ;)
එහෙනම් ඉතින් අදට යන්න වෙලාව හරි methods වල ඉතුරු ටික ඉදිරි පාඩම් වලදී කියල දෙන්නම් එහෙනම් ඉතින් මම ගියා.....
මතක ඇතුව කොමෙන්ටුවකුහ් දාලම යන්න හොදේ.........