1 / 20

Function ( 函數 )

Introduction to the C Programming Language. Function ( 函數 ). Function ( 函數 ). Function 即是副程式 (sub-program, 或 subroutine) 為何要使用 function ? 避免重複寫相同的程式碼 , 節省 coding 時間 可重複使用該程式碼 增加程式的可讀性與可維護性 , 降低主程式之複雜性 開發較大應用程式時 , 可分割成數個不同功能的 function, 由不同的程式設計師分工完成 符合結構化語言之特性

opal
Download Presentation

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. Introduction to the C Programming Language Function (函數)

  2. Function (函數) • Function 即是副程式(sub-program,或 subroutine) • 為何要使用 function ? • 避免重複寫相同的程式碼, 節省coding 時間 • 可重複使用該程式碼 • 增加程式的可讀性與可維護性, 降低主程式之複雜性 • 開發較大應用程式時, 可分割成數個不同功能的function, 由不同的程式設計師分工完成 • 符合結構化語言之特性 • 尚有一些程式語言, 將副程式分為兩類 : • 副程式 : 處理指定功能之程序過程 • 函數式 : 負責計值運算並傳回結果

  3. Function語法規則 • 語法規則 : function_type function_name (parameter, parameter…) { parameters declarations; statements; } • 例: char func(p1, p2) { int p1; char p2; statements; }

  4. 主程式 函數 1 函數 2 函數 1 主程式 函數 1 函數 2 函數 2 主程式 主程式 主程式 函數 1 函數 2 函數 1 函數 2 函數與主程式間之架構 • 合法之架構 • 錯誤之架構

  5. 函式呼叫 範例一 • 比較使用者所輸入兩整數之大小 • void larger_value(a,b) int a, b; { if(a < b) printf("\2: The larger value is %d \n", b); else if(a > b) printf("\2: The larger value is %d \n", a); else printf("\2: Two values are equal. \n"); } void main() { int i, j; printf("\1: Please input 2 values. \n ==> "); scanf("%d %d, &i, &j); larger_value(i,j); }

  6. Function注意事項 • 函數名稱依照識別字的規則命名. • 每個函數各自獨立, 可相互呼叫. • 所有函數語法地位一律平等. • 參數列式呼叫函數時需要與主程式間作資料或位置的傳遞, 若無資料需要傳遞時, 則可省略參數列, 但不可省略小括號. • 若函數須傳回數值時, 則敘述中必有一條傳回敘述, 語法規則為 : return (運算式) • return 為關鍵字 • 運算式可為常數, 變數或算數運算式 • return 除了可把函數內的值傳回呼叫程式外, 同時具有令函數結束, 返回呼叫程式之功能.

  7. 參數列中無資料 函式呼叫 (無參數傳遞) 傳回值 範例二 • 輸入一數值 n, 求出 1 至 n 的連乘 (示範函數傳回數值給呼叫程式) • void main() { long fact(void); long f; f = fact(); printf("The factorial = %ld \n", f); } long fact(void) { int i,n; long f=1; printf("Enter one value : "); scanf("%d", &n); for( i=1; i<= n; i++) f *= i; return(f); }

  8. 參數列中傳遞之參數 傳回值 範例三 • 輸入一數值 n, 求出 1 至 n 的連乘(示範由呼叫程式傳遞一數值給函數, 函數計算後將結果傳回給呼叫程式) • void main() { int n; long f; long fact(int); printf("Enter one value : "); scanf("%d", &n); f = fact(n); printf("The factorial of %d = % ld \n", n, f); } long fact( int n) { int i; long f = 1; for (i =1; i <= n; i++) f *= i; return (f); } 函式呼叫 (參數傳遞)

  9. 範例四 – (共2頁) • 計算輸入之三個整數的 gcd 與 lcm. • void main() { int gcd(int, int); int lcm(int, int); int a, b, c; printf("Enter three integer values : "); scanf("%d %d %d", &a, &b, &c); printf("The gcd of %d %d %d is %d \n", a, b, c, gcd(gcd(a,b), c)); printf("The lcm of %d %d %d is %d \n", a, b, c, lcm(lcm(a,b), c)); }

  10. 範例四 – 續 接上頁 • int gcd(int x, int y) { int 4; while (y != 0) { r = x % y; x = y; y = r; } return(x); } int lcm(int x, int y) { return(x * y / gcd(x,y)); }

  11. 範例五 • 輸入半徑資料計算圓面積 • void main() { float area(int); int r; float a; printf("Enter radius : "); scanf("%d", &r); a = area (r); printf("Circle area = %10.4f \n", a); } float area(int r) { float pi = 3.14159; return(pi * r * r); }

  12. 範例六 (共 2 頁) • 輸入一系列數字, 以函數方式求出最大值與最小值, 將最大值,最小值與數字總和列印出來 • int min(x,y) { if( x<= y) return x; else return y; } int max(x,y) { if(x <= y) return y; else return x; }

  13. 範例六 – 續 接上頁 • void main() { int num, value, i; int tmpmax = -1000; int tmpmin = 1000; int sum = 0; printf("\1: Please input the number of data ==> "); scanf("%d", &num); for( i=1; i <= num; i++) { printf("\1: input value %d ==> ", i); scanf("%d", &value); tmpmin = min(value, tmpmin); tmpmax = max(value, tmpmax); sum += value; } printf("\2: The maximun is %d \n", tmpmax); printf("\2: The minimum is %d \n", tmpmin); printf("\2: The sum is %d \n", sum); }

  14. 陣列資料傳遞 – 範例一 • 利用函數方式, 將陣列中的最小值傳回 • int minimum(var) int var[5]; { int min, i; min = var[0]; for( i=0; i<=4; i++) if(var[i] < min) min = var[i]; return min; } void main() { int array[5]; int minimum_value; int i; for( i =0; i<5; i++) { printf("\1: input value %d ==> ", i+1); scanf("%d", &array[i]); } minimum_value = minimum(array); printf("\2: The minimum value is %d \n", minimum_value); }

  15. 陣列資料傳遞 – 範例二 (共2頁) • 利用函數方式將二維陣列各列的前三元素平均值, 放在最後一個元素位置 • void main() { int num[3][4] = { {88, 29,91, 0}, {86, 84, 90, 0}, {77, 65, 70, 0}}; void average(); int i, j; average(num, 3); for( i=0; i<3; i++) { printf("\2: "); for( j=0; j<4; j++) printf("%5d", num[i][j]); printf("\n"); } }

  16. 陣列資料傳遞 – 範例二 (續) 接上頁 • void average(var, length) int var[][4], length; { int sum, i, j; for( i=0; i<length; i++) { sum = 0; for( j=0; j<4; j++) sum += var[i][j]; var[i][3] = sum / 3; } }

  17. 參數之區域觀念 • 全面性變數(Global variable) : 宣告在主程式區域外的變數, 可在程式中任何區域中存取該變數. • 區域性變數(Local variable) : 宣告在主函數區域中或函數區域中的變數, 只在該宣告的區域中有效. 離開該區域則視為未定義變數, 各區域中的區域變數互不相干.

  18. 參數之儲存類別 • C語言提供四個關鍵字 : auto, extern, static, register • 靜態變數(Static variable) : 只要程式繼續執行, 不因函數的結束而消失, 該變數所配置的記憶體空間與數值依然存在. • 外部變數(Extern variable) : 如上頁提及宣告在主程式區域外的變數為全面性變數, 也稱為外部變數. • 外部靜態變數 : 只提供同一程式檔中的所有函數存取, 其他程式檔中的函數則無法使用. • 暫存器變數(Register variable) : 為加速程式的執行速度, 可將區域性變數或正式參數宣告為暫存器變數.

  19. 範例一 : 區域性變數宣告使用 • void main() { int sum(void); int i; for (i=1; i<=3; i++) printf("%dth time = %d \n", i, sum()); } int sum(void) { int x=0; x++; return(x); }

  20. 靜態變數宣告 範例二 : 靜態性變數宣告使用 • void main() { • int sum(void); • int i; • for (i=1; i<=3; i++) • printf("%dth time = %d \n", i, sum()); • } • int sum(void) • { • static int x=0; • x++; • return(x); • }

More Related