งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

หน่วยที่ 14 การเขียนโปรแกรมย่อย

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "หน่วยที่ 14 การเขียนโปรแกรมย่อย"— ใบสำเนางานนำเสนอ:

1 หน่วยที่ 14 การเขียนโปรแกรมย่อย

2 ฟังก์ชันที่ส่งค่ากลับทางพารามิเตอร์
พารามิเตอร์ธรรมดา มีการแก้ไขไม่ได้ พารามิเตอร์แบบ มีการแก้ไขได้ หาก - นำหน้าด้วย * หรือ - เป็นแอเรย์

3 void AnotherFunction( int a, int b, int *c)
การใช้พารามิเตอร์แบบ มีการแก้ไขได้ มีกฏดังนี้ 1. ที่ส่วนหัวของฟังก์ชัน ให้เขียนเครื่องหมาย ‘*’ หน้าชื่อตัวแปรพารามิเตอร์ เช่น void AnotherFunction( int a, int b, int *c) 2. ในการเปลี่ยนค่าพารามิเตอร์ทุกครั้ง ต้องมีการเขียนเครื่องหมาย ‘*’ นำหน้าตัวแปรพารามิเตอร์ ตัวอย่างเช่น void AnotherFunction( int a, int b, int *c) { *c = (a+b)/2; } 3. ในการเรียกฟังก์ชันทุกครั้ง การส่งพารามิเตอร์แบบ มีการแก้ไข จะต้องเขียนเครื่องหมาย ‘&’ นำหน้าพารามิเตอร์เสมอ เช่น AnotherFunction(2, 4, &x); AnotherFunction (x, 10, &av); AnotherFunction (a, b, &c); 4. ในการเรียกฟังก์ชันทุกครั้ง พารามิเตอร์แบบ มีการแก้ไข จะต้องเป็นตัวแปรเท่านั้น

4 ตัวอย่างที่ 5, ฟังก์ชัน AnotherAverage

5 Give me two integers : 5 26 Average of 5 and 26 is 15.5
/* A program to calculate an average of two numbers */ #include <stdio.h> /* calculate the average value of a and b */ void AnotherAverage ( int a, int b, float *TheAverage ) { float result; result = (float)(a+b)/2 ; *TheAverage = result; } main() int x, y; float av; printf("Give me two integers : "); scanf("%d %d", &x, &y); AnotherAverage(x,y,&av); printf("Average of %d and %d is %0.1f\n", x, y, av); Give me two integers : 5 26 Average of 5 and 26 is 15.5

6 ตัวอย่างที่ 6, ฟังก์ชัน NewAverage

7 /* A program to calculate an average of two numbers */
#include <stdio.h> /* Input two values and calculate the average */ float NewAverage ( int *a, int *b ) { printf("Give me two integers : "); scanf("%d %d", &*a, &*b); return ( (float)(*a + *b)/2.0 ); } main() int n1=1, n2=1; float av; av = NewAverage(&n1, &n2); printf("Average of %d and %d is %0.1f\n", n1, n2, av); Give me two integers : 5 26 Average of 5 and 26 is 15.5

8 ตัวอย่างที่ 7, ฟังก์ชัน swapChar

9 /* A program to swap two characters */ #include <stdio.h>
void swapChar ( char *a, char *b ) { char temp; temp = *a; *a = *b; *b = temp; } /* Main Function */ main() char c1='A', c2='z'; printf("At first, c1 = %c and c2 = %c \n", c1, c2); swapChar(&c1, &c2); printf("After swap, c1 = %c and c2 = %c \n", c1, c2); At first, c1 = A and c2 = z After swap, c1 = z and c2 = A

10 การใช้แอเรย์เป็นพารามิเตอร์ของฟังก์ชัน
ถ้าพารามิเตอร์เป็นแอเรย์ต้องประกาศแอเรย์ให้ชัดเจน เช่น int FindMaxScore ( int Score[100], int NumberOfStudents) โดยถ้าเป็นแอเรย์หนึ่งมิติไม่จำเป็นต้องกำหนดขนาดของแอเรย์ก็ได้ เช่น int FindMaxScore ( int Score[], int NumberOfStudents)

11 2. หากเป็นแอเรย์หลายมิติ ต้องกำหนดขนาดของทุกมิติยกเว้นได้เพียงมิติแรกเท่านั้น เช่น
void GetNames ( char Name[][40], int NumberOfStudents) 3. การส่งแอเรย์ให้กับฟังก์ชัน ทำได้โดยการใช้เพียงชื่อแอเรย์เท่านั้น โดยไม่ต้องมีเครื่องหมายใดๆนำหน้า และไม่ต้องมีวงเล็บตามหลัง FindMaxScore ( TotalScore, NumberOfStudents); GetName(Name, NumberOfStudents);

12 Grade = CalculateGrade(score[i]);
4. แอเรย์ทั้งแอเรย์ถือว่าเป็นพารามิเตอร์แบบ มีการแก้ไขได้ ไม่จำเป็นต้องใช้เครื่องหมาย * และ & 5. อีลีเมนท์เดียวของแอเรย์ถือว่าเป็นตัวแปรเดี่ยว ดังนั้น การส่งเป็นพารามิเตอร์แบบแก้ไขไม่ได้ ต้องระบุหมายเลข Grade = CalculateGrade(score[i]); การส่งเป็นพารามิเตอร์แบบแก้ไขได้ต้องใช้เครื่องหมาย ‘&’ นำหน้า average(2,5,&av[i]);

13 ตัวอย่างที่ 8, ฟังก์ชัน FindMax
/* A program to find maximum value from an array */ #include <stdio.h> /*Find Max Value*/ int FindMax (int Data[], int NumberOfData) { int Max = -1; /* Initialize max to a low value*/ int i; for (i=0; i< NumberOfData; i++) /*Visit every element*/ if (Data[i] > Max) /*If an element is bigger than Max,*/ Max = Data[i]; /*Change Max*/ return(Max); } /* Main Function */ main() int Age[] = {17,18,19,16,20}; int NumberOfPeople = 5,MaxAge; MaxAge = FindMax( Age, NumberOfPeople); printf("The maximum age is %d\n", MaxAge); The maximum age is 20

14 ตัวอย่างที่ 9, ฟังก์ชัน FindCharacter
/* A program to find a character in a string */ #include <stdio.h> #include <string.h> /* Find character c in string Str */ int FindCharacter ( char Str[], char c) { int Position = -1; /*Initialize position to -1*/ int i; for (i=0; i<strlen(Str)&&Position==-1; i++) /*Loop until found */ if (Str[i] == c) /*If an element is equal to c*/ Position = i; /*Change position to i*/ return(Position+1); } /* Main Function */ main() char Name[] = "Suthep Madarasmi"; int FoundPosition = FindCharacter( Name, 'r'); printf("r is found at position %d in %s\n", FoundPosition,Name); r is found at position 12 in Suthep Madarasmi

15 ตัวอย่างที่ 10, ฟังก์ชัน swapInt กับการสลับค่าอีลีเมนท์ของแอเรย์
/* A program to swap two integers */ #include <stdio.h> /* swap two integers */ void swapInt ( int *a, int *b ) { int temp; temp = *a; *a = *b; *b = temp; } /* Main Function */ main() int Age[] = {17, 18, 19, 16, 20}; int NumberOfPeople = 5; printf("Before swap.. Age[2] = %d, Age[4] = %d\n", Age[2],Age[4]); swapInt(&Age[2],&Age[4]); printf("After swap.. Age[2] = %d, Age[4] = %d\n", Age[2],Age[4]); Before sorting.. Age[2] = 19, Age[4] = 20 After sorting.. Age[2] = 20, Age[4] = 19

16 ตัวอย่างที่ 11, ฟังก์ชัน swapInt, Sort และ PrintArray
/* Sort data in array */ void Sort (int Data[], int NumberOfData) { int i,j; for (i=0; i< NumberOfData; i++) /*for each element */ for (j=i+1; j< NumberOfData; j++) /*visit each later element */ if (Data[j] < Data[i]) /*if an element is smaller */ swapInt(&Data[i], &Data[j]); /*swap the elements */ }

17 /* Print data in array */
void PrintArray (int Data[], int NumberOfData) { int i; for (i=0; i< NumberOfData; i++) printf("%3d", Data[i]); printf("\n"); }

18 } main() { int Age[] = {17, 18, 19, 16, 20}; int NumberOfPeople = 5;
printf("Before sorting.. "); PrintArray(Age, NumberOfPeople); Sort(Age, NumberOfPeople); printf("After sorting.. "); } Before sorting After sorting

19 การประกาศฟังก์ชันก่อนเขียนตัวฟังก์ชัน
เขียน prototype ของฟังก์ชันไว้ที่ส่วนหัวของโปรแกรม มีจะลักษณะเดียวกับส่วนหัวของฟังก์ชัน โดยจะใส่ชื่อพารามิเตอร์หรือไม่ก็ได้ #include <stdio.h> void swapInt (int *, int * ); void Sort (int [], int ); void PrintArray (int [], int ); main() { int Age[] = {17, 18, 19, 16, 20}; int NumberOfPeople = 5; printf("Before sorting.. "); PrintArray(Age, NumberOfPeople); Sort(Age, NumberOfPeople); printf("After sorting.. "); } void Sort (int Data[], int NumberOfData) { int i,j; for (i=0; i< NumberOfData; i++) for (j=i+1; j< NumberOfData; j++) if (Data[j] < Data[i]) swapInt(&Data[i], &Data[j]); } void swapInt (int *a, int *b ) int temp; temp = *a; *a = *b; *b = temp; void PrintArray (int Data[],int NumberOfData) int i; printf("%3d", Data[i]); printf("\n");


ดาวน์โหลด ppt หน่วยที่ 14 การเขียนโปรแกรมย่อย

งานนำเสนอที่คล้ายกัน


Ads by Google