Upload
atit-patumvan
View
378
Download
2
Embed Size (px)
DESCRIPTION
Citation preview
Computer ProgrammingChapter 5 : Methods
Atit PatumvanFaculty of Management and Information Sciences
Naresuna University
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
โปรแกรมย่อย (Sub Program)
• โปรแกรมย่อย มีตําแหน่งในการเข้าถึงชุดคําสั่งเพียงตําแหน่งเดียว
• โปรแกรมหลักจะหยุดการทํางาน เมื่อทําการเรียกใช้โปรแกรมย่อย และ
• เมื่อโปรแกรมย่อยทํางานเสร็จสิ้นโปรแกรมหลักจะกลับมาทํางานต่อในชุดคําสั่งที่อยู่ต่อจาก คําสั่งเรียกใช้โปรแกรมย่อย
• โปรแกรมย่อยสามารถแบ่งออกเป็น 2 กลุ่ม
• Procedure เป็นโปรแกรมย่อยที่มีการรับค่าพารามิเตอร์จากโปรแกรมหลัก
• Function เป็นโปรแกรมย่อยที่มีการส่งค่าให้กับโปรแกรมหลัก
2
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
การใหลของชุดคําสั่งในโปรแกรม
3
statements
statements
methods call
statements
statements
methods
statements
statements
Main Program
Sub Program
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
ตัวอย่างการประยุกต์ใช้โปรแกรมย่อย
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
01: public class PrintStar {02: 03: public static void main(String[] args) {04: for (int i = 0; i < 5; i++) {05: star(5);06: }07: }08: 09: public static void star(int x) {10: for (int i = 0; i < x; i++) {11: System.out.print("*");12: }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
Methods
• ในภาษาที่เป็นโครงสร้าง เราสามารถสร้างหน่วยของโปรแกรม (procedure) เพื่อใช้ในการแก้ปัญหาที่ต้องการให้มีการประมวลผลที่คล้ายๆกัน โดยผู้พัฒนาโปรแกรมไม่ต้องเขียนโปรแกรมซ้ําๆหลายครั้ง
• หน่วยของโปรแกรมที่สามารถส่งค่าออกมาเราจะเรียกว่าฟังก์ชัน (function)
• ในภาษาเราจะเรียกหน่วยของโปรแกรมและฟังก์ชัน ด้วยเมธอส (method)
6
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Method Invocation
7
• การเรียกใช้ 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
การเรียก Method ที่มีการส่งค่ากลับ
8
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: }
caller methodcalee method
return value
send value (parameter)
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
Method Overloading
• การที่เมธอสมีชื่อเหมือนกันได้มากกว่าหนึ่งเมธอส
9
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
Parameter Passing: Pass by Value
10
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
Parameter Passing: Pass by Reference
11
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
Scope of Visibility
12
static
instance
method
methodblock
block block