1. Computer Programming
Chapter 5 : Methods
Atit Patumvan
Faculty of Management and Information Sciences
Naresuna University
2. 2
โปรแกรมย่อย (Sub Program)
• โปรแกรมย่อย มีตําแหน่งในการเข้าถึงชุดคําสั่งเพียงตําแหน่งเดียว
• โปรแกรมหลักจะหยุดการทํางาน เมื่อทําการเรียกใช้โปรแกรมย่อย
และ
• เมื่อโปรแกรมย่อยทํางานเสร็จสิ้นโปรแกรมหลักจะกลับมาทํางาน
ต่อในชุดคําสั่งที่อยู่ต่อจาก คําสั่งเรียกใช้โปรแกรมย่อย
• โปรแกรมย่อยสามารถแบ่งออกเป็น 2 กลุ่ม
• Procedure เป็นโปรแกรมย่อยที่มีการรับค่าพารามิเตอร์จาก
โปรแกรมหลัก
• Function เป็นโปรแกรมย่อยที่มีการส่งค่าให้กับโปรแกรมหลัก
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
3. 3
การใหลของชุดคําสั่งในโปรแกรม
Main Program
statements Sub Program
methods
statements
statements
methods call
statements
statements
statements
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
4. 4
ตัวอย่างการประยุกต์ใช้โปรแกรมย่อย
01: public class PrintStar { *****
02:
03: public static void main(String[] args) { *****
04: System.out.println("*****");
05: System.out.println("*****"); *****
06: System.out.println("*****");
07: System.out.println("*****"); *****
08: System.out.println("*****");
09: } *****
10: }
:
03: public static void main(String[] args) {
04: for (int i = 1; i < 6; i++) {
05: System.out.println("*****");
06: }
07: }
:
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
5. 5
ตัวอย่างการประยุกต์ใช้โปรแกรมย่อย
01: public class PrintStar {
02:
03:
04:
public static void main(String[] args) {
for (int i = 0; i < 5; i++) { *****
05:
06: }
star(5);
*****
07:
08:
}
*****
09:
10:
public static void star(int x) {
for (int i = 0; i < x; i++) {
*****
11:
12: }
System.out.print("*");
*****
13: System.out.println();
14: }
15: }
*
: **
03: public static void main(String[] args) {
04: for (int i = 0; i < 5; i++) { ***
05: star(i);
06: } ****
07: }
: *****
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
6. 6
Methods
• ในภาษาที่เป็นโครงสร้าง เราสามารถสร้างหน่วยของโปรแกรม
(procedure) เพื่อใช้ในการแก้ปัญหาที่ต้องการให้มีการประมวลผล
ที่คล้ายๆกัน โดยผู้พัฒนาโปรแกรมไม่ต้องเขียนโปรแกรมซ้ําๆ
หลายครั้ง
• หน่วยของโปรแกรมที่สามารถส่งค่าออกมาเราจะเรียกว่าฟังก์ชัน
(function)
• ในภาษาเราจะเรียกหน่วยของโปรแกรมและฟังก์ชัน ด้วยเมธอส
(method)
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
7. 7
Method Invocation
• การเรียกใช้ Method ทําได้หลายแบบ
• Method ที่ไม่ส่งค่ากลับ: จะต้องถูกเรียกจากตําแหน่งประโยค
• Method ที่มีการการส่งค่ากลับ: จะต้องถูกเรียกจาก term
ภายใน expression
• โครงสร้างการเรียกใช้เป็นดังนี้ <method_name> ([argument list])
01: public class MethodInvocation {
02:
03: public static void main(String[] args) {
04: method();
05: }
06:
07: public static void method() {
08: System.out.println("sub program is called");
09: }
10: }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
8. 8
การเรียก Method ที่มีการส่งค่ากลับ
01: public class FunctionInvocation {
02:
03: public static void main(String[] args) {
04: int x = 7, y = 5;
05: int z = add(x,y);
06: System.out.println(z);
07: }
08:
09: public static int add(int a, int b) {
10: int c = a + b;
11: return c;
12: }
13: }
send value (parameter)
caller method
calee method
return value
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
9. 9
Method Overloading
• การที่เมธอสมีชื่อเหมือนกันได้มากกว่าหนึ่งเมธอส
01: public class MethoadOverload {
02:
03: public static void main(String[] args) {
04: int x = 7, y = 5;
05: double z = 5.0;
06:
07: System.out.println(x / y);
08:
09: System.out.println(div(x, y));
10:
11: System.out.println(div(x, z));
12: }
13:
14: public static double div(int a, int b) {
15: double c = a / (double) b;
16: return c;
17: }
18:
19: public static double div(int a, double b) {
20: double c = a / b;
21: return c;
22: }
23: }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
10. 10
Parameter Passing: Pass by Value
01: public class PassByValue {
02:
03: public static void main(String[] args) {
04: String name = "alice";
05: method(name);
06: System.out.println(name);
07: }
08:
09: public static void method(String arg) {
10: arg = "bob" ;
11: }
12: }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
11. 11
Parameter Passing: Pass by Reference
01: public class PassByReference {
02:
03: public static void main(String[] args) {
04: Person p = new Person();
05: p.setName("alice");
06: method(p);
07: System.out.println(p.getName());
08: }
09:
10: public static void method(Person arg) {
11: arg.setName("bob");
12: }
13: }
14:
15: class Person {
16:
17: private String name;
18:
19: public String getName() {
20: return name;
21: }
22:
23: public void setName(String name) {
24: this.name = name;
25: }
26: }
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
12. 12
Scope of Visibility
static
instance
method
block block
method
block
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University