Title: ??(Function)???
1Introduction to the C Programming Language
2Fucntion
3??Function (??)?
- ????(Function)?
- ???????????,??coding ??
- ?????????,??????
- ?????????????,?????????
- ?????????,???????????function, ?????????????
- ??????????(???,??????)
- ????
- ??????,????????
- ???????,?????????
4???????
- ?????????
- /?????main()???, ??????, ????/
- ????? function_name (????1,????2)
5Function????(1/2)
- ????
- ??????? ????(??????? ????, ??????? ???? )
-
- statements /????/
- return ??? /??????/
-
6Function????(2/2)
- ????1 (????)
- void function_name (???? ??1,???? ??2)
-
- parameters declarations /??????/
- statements /????/
-
- ????2 (???)
- ???? function_name (???? ??1,???? ??2)
-
- parameters declarations /??????/
- statements /????/
- return ???
-
7Example 1a
- ??????,????
- includeltstdio.hgt
- void p(char sn ) /????,????void?????,????ch
ar??/ - int main(void) /???/
-
- char num10
- printf("?????")
- scanf("s",num)
- p(num) /??p??/
- printf("\n")
- system("pause")
-
- void p(char sn ) /p??/
-
- printf("\n\n????? s",sn)
?????
?????
8Example 1b
- ??????,????
- includeltstdio.hgt
- void p(char ) /????,????void?????,????char??
/ - int main(void) /???/
-
- char num10
- printf("?????")
- scanf("s",num)
- p(num) /??p??/
- printf("\n")
- system("pause")
-
- void p(char num )
-
- printf("\n\n????? s",num)
/p???num ??????num ????/
9Example 2
?? ?????,???????,?????????????,????. include
ltstdio.hgt 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??????????
11Function????
- ??????????????.
- ????????,?????.
- ????????????.
- ?????????????????????????, ?????????,???????,
????????. - ?????????, ????????????,????? return (???)
- return ????
- ???????,????????
- return ????????????????,?????????, ?????????.
12???
- return() ???????
- ???????????
- ?return()??????????????????
13??
14???????
- ?????(Global variable) ????????????,
???????????????. - ?????(Local variable) ??????????????????,
???????????. ?????????????, ?????????????.
includeltstdio.hgt /????,?????/ ???? ????
/????/ int main( ) ???? /????/ return
0 /???/
15???????
- C????????? auto, extern, static, register
- ????auto ?????????????.
- ????(Static variable) ????????, ??????????,
???????????????????. - ????(Extern variable) ???????????????????????,
???????. - ?????? ????????????????, ??????????????.
- ?????(Register variable)???????????, ???????2?,
???2?, ??????????????auto????.
16??? ?????????
includeltstdio.hgt includeltstdlib.hgt int
sum(void) void main(void) int i /????/
for (i1 ilt3 i) printf("dth time
d \n", i, sum()) system("pause")
int sum(void) int x0 /????/ x
return(x)
17??? ?????????
includeltstdio.hgt includeltstdlib.hgt void main()
int sum(void) int i /????/ for
(i1 ilt3 i) printf("dth time d \n",
i, sum()) system("pause") int sum(void)
static int x0 x return(x)
18??? ?????????
includeltstdio.hgt includeltstdlib.hgt int
sum(void) int x0 /????/ void main(void)
int i /????/ x for (i1 ilt3
i) printf("dth time d \n", i,
sum()) system("pause") int
sum(void) x return(x)