a = function ( x , y ) ;
表示將變數 x , y 輸入函數 function , 其傳回函數值設定給變數 a 。
儲存類別 資料型別 函數名稱 (形式引數的串列) { 形式引數的宣告 ; 函數的本體(執行敘述) } |
Example: void Line (void) { int j; for (j=1;j<20;j++) printf(“%d\n”,j); } |
函 數 的 儲 存 類 別 |
|||
儲存類別 |
功能敘述 | ||
static |
只能在進行宣告的編譯單位內可以執行 | ||
extern |
在進行宣告的編譯單位以外也可執行 | ||
Example1: file1.c extern func1( ) { . . } static func2( ) { . } |
Example2: file2.c extern func1( ); static func3( ) { . . func1( );. } |
return ( 變數或式子 ) ;
此 處 必 須 先 宣 告 傳 回 值 (函數值) 的 資 料 形 態 是 什 麼 。
Example 1: 傳值呼叫 #include < stdio.h > int func(a,b,c) int a,b,c; { int d; d=a+b+c; return(d); } void main(void) { int x,y,z,p; x=y=z=3; p=func(x,y,z); printf("p-->%d\n",p); } result : p-->9 |
Example 2: 傳址呼叫 #include < stdio.h > void func(a,b,c,d)int *a,*b,*c,*d;{ *d=*a+*b+*c;} void main(void){ int x,y,z,p; x=y=z=3; func(&x,&y,&z,&p); printf("p-->%d\n",p); } result : p-->9 |
/* ================================================== *//* 程式實例:傅值和傳址的副程序呼叫(交換XY) *//* ================================================== */ /* --------------------交換兩值(傳值副程序)-------------------- */void swap1(int x,int y){ int temp; temp = x; x = y; y = temp;} /* ------------------- 交換兩值(傳址副程序) ----------------- */void swap2(int *x,int *y){ int temp; temp = *x; *x = *y; *y = temp;} /* ---------------------主程式---------------------- */void main(){ int x = 10, y=0; /* 局部變數宣告 */ printf(" X Y \n"); printf(" 初值 %d %d \n",x,y); swap1(x,y); /* 傳值副程序呼叫(交換XY) */ printf(" 傳值 %d %d \n",x,y); swap2(&x,&y); /* 傳址副程序呼叫(交換XY) */ printf(" 傳址 %d %d \n",x,y);} | ||||||||||||
|
歡迎光臨 中央論壇 - CENTER BBS (https://www.centerbbs.com/) | Powered by Discuz! X3 |