1 / 18

函式 (Function) 、參數

Introduction to the C Programming Language. 函式 (Function) 、參數. Fucntion. ( 函數、函式、副程式 ). 何謂 Function ( 函式 )?. 何謂函式 (Function)? 避免重複寫相同的程式碼 , 節省 coding 時間 可重複使用該程式碼 , 容易偵錯程式 增加程式的可讀性與可維護性 , 降低主程式之複雜性 開發較大應用程式時 , 可分割成數個不同功能的 function, 由不同的程式設計師分工完成 符合結構化語言之特性 ( 模組化 , 由上而下設計 ) 函式特點

lois-boone
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. Fucntion (函數、函式、副程式)

  3. 何謂Function (函式)? • 何謂函式(Function)? • 避免重複寫相同的程式碼,節省coding 時間 • 可重複使用該程式碼,容易偵錯程式 • 增加程式的可讀性與可維護性,降低主程式之複雜性 • 開發較大應用程式時,可分割成數個不同功能的function, 由不同的程式設計師分工完成 • 符合結構化語言之特性(模組化,由上而下設計) • 函式特點 • 不可單獨執行,為主程式之一部份 • 擁有自己的名稱,不可與其他函數同名

  4. 宣告函式1 主程式 函式1 函式1 主程式 宣告函式的格式 • 函式原型宣告的格式 /*當函式位於main()之前時, 不需宣告函式, 反之則要*/ 傳回值型態 function_name (資料型態1,資料型態2…);

  5. Function語法規則(1/2) • 語法規則 傳回值資料型態 函數名稱(傳入值資料型態變數名稱,傳入值資料型態變數名稱…) { statements; /*函式主體*/ return 傳回值; /*若需傳回值時*/ }

  6. Function語法規則(2/2) • 語法規則1 : (不傳回值) voidfunction_name (資料型態 參數1,資料型態 參數2…) { parameters declarations; /*區域變數宣告*/ statements; /*函式主體*/ } • 語法規則2 :(傳回值) 資料型態 function_name (資料型態 參數1,資料型態 參數2…) { parameters declarations; /*區域變數宣告*/ statements; /*函式主體*/ return 傳回值; }

  7. Example 1a 範例:呼叫函式,印出學號 #include<stdio.h> void p(char sn[ ]); /*宣告函式,函式原型void是不傳回值,傳入一個char型態*/ int main(void) /*主程式*/ { char num[10]; printf("請輸入學號:"); scanf("%s",&num); p(num); /*呼叫p函式*/ printf("\n"); system("pause"); } void p(char sn[ ]) /*p函式*/ { printf("\n\n您的學號為 %s",sn); } 進入副程式 離開副程式

  8. Example 1b 範例:呼叫函式,印出學號 #include<stdio.h> void p(char *); /*宣告函式,函式原型void是不傳回值,傳入一個char型態*/ int main(void) /*主程式*/ { char num[10]; printf("請輸入學號:"); scanf("%s",&num); p(num); /*呼叫p函式*/ printf("\n"); system("pause"); } void p(char num[ ]) { printf("\n\n您的學號為 %s",num); } /*p函式的num[ ]與主程式裡的num[ ]是不同地*/

  9. Example 2 範例: 寫兩個函式,一個使兩數相加,傳回運算後結果;一個顯示答案,不傳回值. #include <stdio.h> int add(int,int);/*宣告函式,函式原型 int是傳回int型態,傳入兩個int型態 */ void show(int); /*宣告函式,函式原型void是不傳回值,傳入一個int型態 */  void main() { int nNum1 = 5 , nNum2 = 5 , nSum; nSum = add(nNum1,nNum2);/*把add(nNum1,nNum2)的return傳回nSum */ show(nSum); /*把nSum傳入show函式 */  } /*傳入的值是nNum1,nNum2,定義兩個int且nData1 = nNum1, nNum2 = nData2 */  int add(int nData1,int nData2) { return (nData1 + nData2);        /*傳回nData1 + nData2相加的值 */  } /*傳入的是nSum,定義一個int nData,且nData= nSum; */  void show(int nData) { printf("The Anser is %d" , nData); }

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

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

  12. 傳回值 • return() 敘述之目的如下 • 將控制權傳回給呼叫程式 • 將return()敘述後括號內之數值傳給呼叫程式之方式

  13. 參數

  14. 參數之區域觀念 • 全面性變數(Global variable) : 宣告在主程式區域外的變數, 可在程式中任何區域中存取該變數. • 區域性變數(Local variable) : 宣告在主函數區域中或函數區域中的變數, 只在該宣告的區域中有效. 離開該區域則視為未定義變數, 各區域中的區域變數互不相干. #include<stdio.h> /*前置處理,呼叫標頭檔*/ 宣告函式 宣告變數 /*全域變數*/ int main( ) { 宣告變數 /*區域變數*/ return 0; /*傳回值*/ }

  15. 參數之儲存類別 • C語言提供四個關鍵字 : auto, extern, static, register • 區域變數:auto 宣告的變數必須包含於函數內. • 靜態變數(Static variable) : 只要程式繼續執行, 不因函數的結束而消失, 該變數所配置的記憶體空間與數值依然存在. • 外部變數(Extern variable) : 如上頁提及宣告在主程式區域外的變數為全面性變數, 也稱為外部變數. • 外部靜態變數 : 只提供同一程式檔中的所有函數存取, 其他程式檔中的函數則無法使用. • 暫存器變數(Register variable):只適用於區域變數的宣告, 且個數不能超過2個, 若超過2個, 則會自將超過的暫存器變數轉成auto區域變數.

  16. 範例一 : 區域性變數宣告使用 #include<stdio.h> #include<stdlib.h> int sum(void); void main(void) { int i; /*區域變數*/ for (i=1; i<=3; i++) printf("%dth time = %d \n", i, sum()); system("pause"); } int sum(void) { int x=0; /*區域變數*/ x++; return(x); }

  17. 靜態變數宣告 範例二 : 靜態性變數宣告使用 #include<stdio.h> #include<stdlib.h> void main() { int sum(void); int i; /*區域變數*/ for (i=1; i<=3; i++) printf("%dth time = %d \n", i, sum()); system("pause"); } int sum(void) { static int x=0; x++; return(x); }

  18. 範例一 : 全域性變數宣告使用 #include<stdio.h> #include<stdlib.h> int sum(void); int x=0; /*全域變數*/ void main(void) { int i; /*區域變數*/ x++; for (i=1; i<=3; i++) printf("%dth time = %d \n", i, sum()); system("pause"); } int sum(void) { x++; return(x); }

More Related