1 / 24

Function

Function. ธนวัฒน์ แซ่เอียบ. What is a function. ฟังก์ชันในภาษา C เป็นโปรแกรมที่ถูกออกแบบมาเพื่อใช้แก้ปัญหางานใดงานหนึ่งโดยเฉพาะ ฟังก์ชันจะเปลี่ยน input ให้กลายเป็น output โดยที่ผู้ใช้ไม่รู้ว่าภายในฟังก์ ชันเกิดอะไรขึ้นบ้าง ยกเว้นคนที่เป็นเจ้าของฟังก์ชันนั้น

zena-kemp
Télécharger la présentation

Function

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Function ธนวัฒน์ แซ่เอียบ

  2. What is a function • ฟังก์ชันในภาษา C เป็นโปรแกรมที่ถูกออกแบบมาเพื่อใช้แก้ปัญหางานใดงานหนึ่งโดยเฉพาะ • ฟังก์ชันจะเปลี่ยน input ให้กลายเป็น output โดยที่ผู้ใช้ไม่รู้ว่าภายในฟังก์ชันเกิดอะไรขึ้นบ้าง ยกเว้นคนที่เป็นเจ้าของฟังก์ชันนั้น • Output ที่ได้จากฟังก์ชันสามารถเป็น action และ value • ตัวอย่างฟังก์ชันที่ output เป็น action เช่น printf() • ตัวอย่างฟังก์ชันที่ output เป็น value เช่น getchar()

  3. Defining a function #include <stdio.h> void printMessage (void) { printf ("Programming is fun.\n"); } int main (void) { printMessage (); printMessage (); return 0; } • Function • Definition • occurs ONE time for all • outside other functions • Function • calls (invocations) • occurs ANY (0-N) times • statement inside (other) functions body

  4. Transfer of control flow printf main printMesage printMessage (); printMessage (); return 0; printf เมื่อพบคำสั่งการเรียกใช้ฟังก์ชัน โปรแกรมจะเปลี่ยนลำดับไปที่ฟังก์ชัน แล้วปฏิบัติฟังก์ชันนี้จนจบ หลังจากนั้นจะกลับไปที่ฟังก์ชันผู้เรียก และปฏิบัติงานต่อจากคำสั่งการเรียกใช้ฟังก์ชัน

  5. Function definitions General form of function definition: return-type function-name(argument declarations) { declarations and statements } return-type arguments void printMessage ( void ) { printf ("Programming is fun.\n"); } void printMessage (void) { printf ("Programming is fun.\n"); return; }

  6. Function prototype • นำบรรทัดแรกของการนิยามฟังก์ชัน มาเขียนเป็นต้นแบบของฟังก์ชัน (สังเกตว่าจะมีข้อมูลที่จำเป็นต้องรู้เกี่ยวกับฟังก์ชันไว้ทั้งหมด) void calculateTriangularNumber (int n) • Input ของฟังก์ชันประกอบด้วย พารามีเตอร์หรืออาร์กีเมนต์ • ในการนิยามฟังก์ชัน เรียกว่า formal parameters เป็นชื่อสำหรับใช้ภายในฟังก์ชัน • ในคำสั่งเรียกฟังก์ชัน เรียกว่า actual parameters • actual parameters สามารถเป็นค่าคงที่ ตัวแปร หรือ expression • actual parameters จะถูกหาค่า และนำค่าที่ได้ส่งต่อให้กับ formal parameter ของฟังก์ชันที่สัมพันธ์กัน

  7. Example: function declaration #include <stdio.h> void printMessage (void) ; int main (void) { printMessage (); printMessage (); return 0; } void printMessage (void) { printf ("Programming is fun.\n"); } Function declaration (prototype) (has to be before the calling function) Function calls Function definition (can be after the calling function)

  8. Examples: function declarations • การประกาศฟังก์ชันสามารถระบุเพียงแค่ type ไม่จำเป็นต้องระบุชื่อก็ได้ int gcd (int u, int v); Or int gcd (int, int); void calculateTriangularNumber (int n); Or void calculateTriangularNumber (int);

  9. Example: arguments • // Function to calculate the nth triangular number • #include <stdio.h> • void calculateTriangularNumber ( int n ) • { • int i, triangularNumber = 0; • for ( i = 1; i <= n; ++i ) • triangularNumber += i; • printf ("Triangular number %i is %i\n", n, triangularNumber); • } • int main (void) • { • calculateTriangularNumber (10); • calculateTriangularNumber (20); • calculateTriangularNumber (50); • return 0; • }

  10. Arguments and local variables • ตัวแปรที่นิยามภายในฟังก์ชันเรียกว่า local variables • ตัวแปรแบบนี้สามารถเข้าถึงได้เฉพาะฟังก์ชันนี้เท่านั้น ฟังก์ชันอื่นไม่สามารถเข้ามาใช้ได้ • ถ้ามีการกำหนดค่าเริ่มต้นให้กับตัวแปร แต่ละครั้งที่มีการเรียกฟังก์ชันจะกำหนดค่าเริ่มต้นนี้ให้กับตัวแปรทุกครั้ง • Formal parameters เหมือนกับ local variables • Scope คือพื้นที่ที่สามารถเข้าถึงตัวแปรได้ • Scope ของ local variables and formal parameters อยู่ภายในฟังก์ชันเท่านั้น

  11. exercise • จากตัวอย่างก่อนหน้านี้ ภายในฟังก์ชัน calculateTriangularNumber (10); บรรทัดสุดท้าย ค่าของ n, I, triangularNumber มีค่าเท่ากับเท่าไร • จากตัวอย่างก่อนหน้านี้ ภายในฟังก์ชัน calculateTriangularNumber (20); บรรทัดสุดท้าย ค่าของ n, I, triangularNumber มีค่าเท่ากับเท่าไร • จากตัวอย่างก่อนหน้านี้ ภายในฟังก์ชัน calculateTriangularNumber (50); บรรทัดสุดท้าย ค่าของ n, I, triangularNumber มีค่าเท่ากับเท่าไร

  12. Example: arguments • #include <stdio.h> • void gcd (int u, int v) • { • int temp; • printf ("The gcd of %i and %i is ", u, v); • while ( v != 0 ) { • temp = u % v; • u = v; • v = temp; • } • printf ("%i\n", u); • } • int main (void) • { • gcd (150, 35); • gcd (1026, 405); • gcd (83, 240); • return 0; • } ถ้าต้องมี parameter มากกว่า 1 ตัวต้องประกาศ type แต่ละตัวให้ชัดเจน

  13. Example: arguments are passed by copying values ! • #include <stdio.h> • void gcd (int u, int v) • { • int temp; • printf ("The gcd of %i and %i is ", u, v); • while ( v != 0 ) { • temp = u % v; • u = v; • v = temp; • } • printf ("%i\n", u); • } • int main (void) • { • int x=10,y=15; • gcd (x, y); • printf(“x=%i y=%i \n”,x,y); • return 0; • } u และ v สามารถถูกกำหนดค่าใหม่ในฟังก์ชันได้ x และ y ค่าคงเดิม ไม่มีการเปลี่ยนแปลง

  14. Example: arguments are passed by copying values ! Ex จงแสดงสิ่งที่พิมพ์ออกทางจอภาพ • #include <stdio.h> • void multiplyBy2 (float x) • { • printf(“parameter at start: %.2f, at %p \n”,x, &x); • x*=2; • printf(“parameter at end: %.2f, at %p \n”,x, &x); • } • int main (void) • { • float y = 7; • printf (“y before call: %.2f, at %p \n", y, &y); • multiplyBy2 (y); • printf (“y after call: %.2f, at %p \n", y, &y); • return 0; • }

  15. Example: scope of local variables Ex จงแสดงสิ่งที่พิมพ์ออกทางจอภาพ #include <stdio.h> void f1 (float x) { int n=6; printf(“%f \n”, x+n); } int f2(void) { float n=10; printf(“%f \n”,n); } int main (void) { int n=5; f1(3); f2(); return 0; }

  16. Returning function results • ฟังก์ชันภาษา C สามารถส่งค่ากลับได้ เช่น return expression • ค่าที่ได้จาก expression จะส่งกลับไปให้ฟังก์ชัน ถ้า type ของ expression ไม่ตรงกับ return type ตามการประกาศฟังก์ชัน ภาษา C จะเปลี่ยน type ตามที่ได้ประกาศไว้ให้ • รูปแบบง่ายๆของ return คือ return; • การปฏิบัติคำสั่ง return statement จะทำให้โปรแกรมกลับไปฟังก์ชันผู้เรียกทันที

  17. Example: function result • จงเขียนฟังก์ชันหาค่าสูงที่สุดของพารามิเตอร์ • #include <stdio.h> • int max (int u, int v) • { • … • } • int main (void) • { • int result; • result = max (150, 35); • printf ("%i\n", result); • result = max (1026, 405); • printf ("%i\n", result); • printf ("%i\n", gcd (83, 240)); • return 0; • }

  18. Parameter are passed by reference • ถ้าภายในฟังก์ชันเปลี่ยนแปลงค่าของพารามิเตอร์แล้ว จะส่งผลให้พารามิเตอร์ที่ส่งเข้ามาถูกเปลี่ยนแปลงค่าไปด้วย • ใช้ type แบบ pointer void increment(int *n){ *n=*n+1; } void main(){ int a=2; increment(…); }

  19. Passing arrays as parameters • A whole array can be one parameter in a function • In the function declaration, you can then omit the specification of the number of elements contained in the formal parameter array. • The C compiler actually ignores this part of the declaration anyway; all the compiler is concerned with is the fact that an array is expected as an argument to the function and not how many elements are in it. • Example: a function that returns the minimum value from an array given as parameter • int minimum (int values[10]); • We must modify the function definition if a different array size is needed ! • int minimum (int values[]); • Syntactically OK, but how will the function know the actual size of the array ?! • int minimum (int values[], int numberOfElements);

  20. Example: Passing arrays as parameters • // Function to find the minimum value in an array • #include <stdio.h> • int minimum (int values[10]) { • int minValue, i; • minValue = values[0]; • for ( i = 1; i < 10; ++i ) • if ( values[i] < minValue ) • minValue = values[i]; • return minValue; • } • int main (void) { • int scores[10], i, minScore; • printf ("Enter 10 scores\n"); • for ( i = 0; i < 10; ++i ) • scanf ("%i", &scores[i]); • minScore = minimum (scores); • printf ("\nMinimum score is %i\n", minScore); • return 0; • }

  21. Example: No size specified for formal parameter array • // Function to find the minimum value in an array • #include <stdio.h> • int minimum (int values[], int numberOfElements) • { • int minValue, i; • minValue = values[0]; • for ( i = 1; i < numberOfElements; ++i ) • if ( values[i] < minValue ) • minValue = values[i]; • return minValue; • } • int main (void) • { • int array1[5] = { 157, -28, -37, 26, 10 }; • int array2[7] = { 12, 45, 1, 10, 5, 3, 22 }; • printf ("array1 minimum: %i\n", minimum (array1, 5)); • printf ("array2 minimum: %i\n", minimum (array2, 7)); • return 0; • }

  22. Array parameters are passed by reference ! • If a function changes the value of an array element, that change is made to the original array that was passed to the function. This change remains in effect even after the function has completed execution and has returned to the calling routine. • Parameters of non-array type: passed by copying values • Parameters of array type: passed by reference • the entire contents of the array is not copied into the formal parameter array. • the function gets passed information describing where in the computer’s memory the original array is located. • Any changes made to the formal parameter array by the function are actually made to the original array passed to the function, and not to a copy of the array.

  23. Example: Array parameters are passed by reference ! • #include <stdio.h> • void multiplyBy2 (float array[], int n) • { • int i; • for ( i = 0; i < n; ++i ) • array[i] *= 2; • } • int main (void) • { • float floatVals[4] = { 1.2f, -3.7f, 6.2f, 8.55f }; • int i; • multiplyBy2 (floatVals, 4); • for ( i = 0; i < 4; ++i ) • printf ("%.2f ", floatVals[i]); • printf ("\n"); • return 0; • }

  24. ที่มา • conf.dr.ing. Ioana Sora : University of Timisoara, Romania

More Related