




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、會計學(xué)1C語言程序設(shè)計中國水利水電語言程序設(shè)計中國水利水電AB函數(shù)函數(shù)第4章 函數(shù)函數(shù)頭函數(shù)頭函數(shù)體函數(shù)體第1頁/共43頁#include int max(int x, int y);void main() int a,b,c; cout a b; c = max(a,b); cout a,b中較大的數(shù)是: c y) m=x; else m=y; return m; 第4章 函數(shù)函數(shù)調(diào)用語句函數(shù)調(diào)用語句主調(diào)函數(shù)主調(diào)函數(shù)被調(diào)函數(shù)被調(diào)函數(shù)第2頁/共43頁第4章 函數(shù)執(zhí)行該語句時,不帶回返回執(zhí)行該語句時,不帶回返回值,只是返回主調(diào)函數(shù)值,只是返回主調(diào)函數(shù)第3頁/共43頁第4章 函數(shù)第4頁/共43頁第
2、4章 函數(shù)第5頁/共43頁#include double power(double x, int n);void main() double x; int n; cout x n; cout x 的的 n 次方是:次方是: power(x,n) endl;double power(double x, int n) double a = 1.0; int i; for(i=1; i=n; i+) a *= x; return a; 第4章 函數(shù) 返 回第6頁/共43頁第4章 函數(shù)第7頁/共43頁#include void swap(int x, int y);void main()int a,
3、b;a = 10;b = 20;swap(a, b);cout a , b endl;10a10 x20b20y1020 xy10temp2020 xy10temp2010 xy10tempvoid swap(int x, int y)int temp;temp = x;x = y;y = temp;第4章 函數(shù)注意:值傳遞時,函數(shù)的實參與形參在內(nèi)存中占用不同的存儲空間,值只能由注意:值傳遞時,函數(shù)的實參與形參在內(nèi)存中占用不同的存儲空間,值只能由實參傳遞給形參,而形參的變化并不會影響實參。因此不能完成程序功能。實參傳遞給形參,而形參的變化并不會影響實參。因此不能完成程序功能。第8頁/共43頁第
4、4章 函數(shù)第9頁/共43頁#include void main()int a=1;int c=10;int &b = a;cout a , b , c endl;b = c;cout a , b , c endl;b = 20;cout a , b , c endl;定義引用定義引用b,并將其作為,并將其作為a的別名的別名第4章 函數(shù)將將c的值賦給的值賦給b,不是將,不是將b作為作為c的別名的別名第10頁/共43頁第4章 函數(shù)#include void swap(int x, int y);void main()int a, b;a = 10;b = 20;swap(a, b);cou
5、t a , b endl;void swap(int &x, int &y)int temp;temp = x;x = y;y = temp;第11頁/共43頁第4章 函數(shù)10axby20axby10temp20axby10temp10axby10temp20202010注意:引用作參數(shù)時,函數(shù)的實參與形參在內(nèi)存中共用存儲單元,因此形參的注意:引用作參數(shù)時,函數(shù)的實參與形參在內(nèi)存中共用存儲單元,因此形參的變化會使實參同時變化。變化會使實參同時變化。 返 回第12頁/共43頁第4章 函數(shù)第13頁/共43頁#include long f1(int n);long f2(int m)
6、;void main()int n;long s;cout n;s = f1(n);cout s endl;第4章 函數(shù)long f1(int n) / 求求1!+2!+n!int i;long sum = 0;for(i=1; i=n; i+)sum += f2(i);return sum;long f2(int m) / 求求m!int i;long s = 1;for(i=1; i=m; i+)s *= i;return s;第14頁/共43頁#include int fun2(int m);int fun1(int x,int y);void main(void) int a,b; c
7、out a b; cout a、b的平方和:的平方和: fun1(a,b) endl;int fun1(int x, int y)return ( fun2(x) + fun2(y) );int fun2(int m)return ( m*m );第4章 函數(shù) 返 回第15頁/共43頁第4章 函數(shù)f函數(shù)調(diào)用f函數(shù)f1函數(shù)調(diào)用f2函數(shù)f2函數(shù)調(diào)用f1函數(shù)直接調(diào)用間接調(diào)用第16頁/共43頁#include long power(int n);void main() int n; long y; cout n; y=power(n); cout n != y 1) f=n*power(n-1); e
8、lse f=1; return f;第17頁/共43頁第4章 函數(shù)第18頁/共43頁第4章 函數(shù)ABC第19頁/共43頁#include void Move(char x, char y);void Hanoi(int n, char one, char two, char three);void main() int n; cout n; cout n 個盤子的移動過程為: endl; Hanoi(n, A, B, C);/函數(shù)Move()將一個盤子從x針移到y(tǒng)針void Move(char x, char y) cout x y endl;第4章 函數(shù)第20頁/共43頁/函數(shù)Hanoi()
9、將n-1個盤子從one針借助two針移到three針void Hanoi(int n, char one, char two, char three) if(n=1) Move(one, three); else Hanoi(n-1, one, three, two); Move(one, three); Hanoi(n-1, two, one, three); 第4章 函數(shù) 返 回運(yùn)行演示運(yùn)行演示第21頁/共43頁第4章 函數(shù)第22頁/共43頁#include inline int Add(int a, int b)int x;x = a+b;return x;第4章 函數(shù) 返 回void
10、main()int a, b, c;a = 10;b = 20;c = Add(a,b);cout a + b = c endl;c = Add(a,50);cout a + 50 = c endl;c = Add(50,50);cout 50 + 50 = c endl;第23頁/共43頁第4章 函數(shù)第24頁/共43頁#include int max(int x, int y); double max(double x, double y); void main() int a=10, b=20 ,c; double x=200.3, y=400.6, z; c = max(a,b); z
11、= max(x,y); cout c z endl; 第4章 函數(shù) 返 回int max(int x, int y) cout int function y) return x; else return y; double max(double x, double y) cout float function y) return x; else return y; 第25頁/共43頁第4章 函數(shù)#include double power(double x=10.0, int n=2); void main() cout power(3, 5) endl; cout power(3) endl;
12、 cout power() endl; double power(double x, int n) int i;double s=1.0;for(i=1; i=n; i+) s *= x;return s; 第26頁/共43頁第4章 函數(shù)第27頁/共43頁第4章 函數(shù)#include int add(int x=5, int y=6); float add(int x=5, float y=10.0); void main() int a; float b; a= add(10,20); b= add(10); cout a= a endl; cout b= b endl; int add(i
13、nt x, int y) return x+y; float add(int x, float y) return x+y; 返 回第28頁/共43頁第4章 函數(shù)void f1(int a) int b,c; void main() int m,n; int i,a;for(i=0; i10; i+) int b; 第29頁/共43頁第4章 函數(shù)int a,b; void f1( ) int x,y;void main() 第30頁/共43頁#include int i=1; /全局變量,文件作用域 void main() cout全局變量 i=iendl; /輸出1 int i=5; /函數(shù)
14、局部變量,塊作用域 int i; /塊局部變量,塊作用域 i=7; cout塊局部變量 i=iendl; /輸出7 cout全局變量 i=:iendl; /輸出1,:使用全局變量 cout函數(shù)局部變量 i=iendl; /輸出5 cout全局變量 i=:iendl; /輸出1,:使用全局變量 第4章 函數(shù) 第31頁/共43頁第4章 函數(shù)第32頁/共43頁#include int fact( int n);void main() int i;for(i=1; i=4; i+) cout i ! = fact(i) endl;int fact(int n) static int f=1;/僅在第一
15、次調(diào)用函數(shù)時執(zhí)行一次f *= n;return f;第4章 函數(shù) 第33頁/共43頁第4章 函數(shù)第34頁/共43頁#include void other(void); int i=1; / i 為全局變量,具有靜態(tài)生存期。 void main(void) static int a; / a為靜態(tài)局部變量,具有全局壽命,局部可見。 int b=-10; / b, c為動態(tài)局部變量,具有局部生存期。 int c=0; cout-MAIN-n; cout i: i a: a b: b c: cendl; c=c+8; other(); cout-MAIN-n; cout i: i a: a b: b c: cendl; i=i+10; other(); 第4章 函數(shù)第35頁/共43頁void other(void) / a,b為靜態(tài)局部變量,具有全局壽命,局部可見,/ 只第一次進(jìn)入函數(shù)時被初
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中國桑葉項目創(chuàng)業(yè)計劃書
- 中國肉用仔雞飼料項目創(chuàng)業(yè)計劃書
- 中國康復(fù)醫(yī)療機(jī)械項目創(chuàng)業(yè)計劃書
- 中國計算機(jī)系統(tǒng)排除故障項目創(chuàng)業(yè)計劃書
- 中國干香菇項目創(chuàng)業(yè)計劃書
- 中國鵝養(yǎng)殖業(yè)項目創(chuàng)業(yè)計劃書
- 乙炔鋼瓶試題及答案
- 樂山保安考試題及答案
- 家具定制配送安裝合同協(xié)議
- 小學(xué)五年級上冊作文
- 西藏特色美食文化介紹推介PPT圖文課件
- 路燈養(yǎng)護(hù)投標(biāo)方案(技術(shù)方案)
- 國家開放大學(xué)電大本科《管理英語4》期末試題題庫及答案(試卷號:1389)
- 護(hù)理文書質(zhì)控PDCA工作匯報
- 詢價投標(biāo)文件(范本)
- 手術(shù)室PDCA-提高急診手術(shù)器械物品準(zhǔn)備的完善率
- 幼兒園大班心理健康《我勇敢了》課件
- 單光纖光鑷數(shù)值仿真和光阱力計算的中期報告
- 有害物質(zhì)管理程序
- 動火作業(yè)培訓(xùn)課件
- 法學(xué)專業(yè)實習(xí)手冊
評論
0/150
提交評論