版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、第3章 函數(shù)和作用域,3.1 函數(shù)的定義和說明 3.2 函數(shù)的調(diào)用 3.3 函數(shù)的參數(shù)傳遞 3.4 內(nèi)聯(lián)函數(shù) 3.5 帶默認(rèn)參數(shù)的函數(shù) 3.6 函數(shù)重載 3.7 使用C+系統(tǒng)函數(shù) 3.8 作用域,【 3.1 函數(shù)的定義和說明】,【 3.1.1 函數(shù)的定義】,聲明函數(shù),就是告訴編譯器函數(shù)的名稱、類型和形式參數(shù)。,定義函數(shù),就是告訴編譯器函數(shù)所做的工作。,在C+程序中,定義一個函數(shù)的格式如下: 類型 函數(shù)名(形式參數(shù)表) 語句序列 ,形式參數(shù)表由0個、1個或多個參數(shù)組成,內(nèi)容如下: 類型1 形式參數(shù)名1,類型2 形式參數(shù)名2,類型n 形式參數(shù)名n,例 3.1,聲明函數(shù),一般采用聲明函數(shù)原型。形式如
2、下: 類型 函數(shù)名(形式參數(shù)表);,例如: double rectanglearea(double a,double b); double rectanglearea(double,double);,【 3.2 函數(shù)的調(diào)用】,【 3.1.2 函數(shù)的聲明】,函數(shù)的調(diào)用的一般形式如下: 函數(shù)名(實際參數(shù)表),函數(shù)調(diào)用:,函數(shù)的返回值是通過返回語句return來實現(xiàn)的。 return語句的一般格式如下: return 表達(dá)式;,例 3.2,double rectanglearea(double a, double b); /函數(shù)聲明 void main() double length, width;
3、 coutlengthwidth; cout the area of the rectangle is rectanglearea (length, width); / length和width是實參,分別將各自的值賦給形參a和b coutendl; double rectanglearea(double a, double b) / a和b是形參 double s; s = a * b; return s; ,int a = 20; int s; s = area(+a, a * 2); coutsendl; int area(int a, int b) return (a * b); ,例
4、 3.4,【 3.3 函數(shù)的參數(shù)傳遞 】,cout x= x t y = yendl; swap(x, y); cout In main function: endl; cout After swap: ; cout x= x t y = yendl; void swap(int a, int b) int temp; cout In Swap function: endl; cout Before swap: ; cout a= at b= bendl; temp = a; a = b; b = temp; cout After swap: ; cout a= a t b= bendl; ,
5、【 3.4 內(nèi)聯(lián)函數(shù)】,形式如下: inline 類型 函數(shù)名(形參表) ./函數(shù)體 ,【 3.5 帶默認(rèn)形參值的函數(shù)】,例如: double area(double a=2.6,double b=4); double area(double a,double b=4); double area(double=2.6,double =4);,double area(double a=2.6,double b); double area(double =2.6,double b,double=4);,int area; area = volume(l, w, h); coutendlthe ar
6、ea of cube equal:t area; coutendlendl; area = volume(l, w); /h取默認(rèn)的值 coutendl the area of cube equal:tarea; coutendlendl; area = volume(l); /w和h取默認(rèn)的值 coutendl the area of cube equal:tarea; coutendlendl; int volume(int length, int width, int height) cout The information of the cube is:tlength=t length
7、; cout twidth=twidth theight=t height; return (length*width*height); ,例 3.8,if(number=2) coutlength2width2; coutendl; total = totalarea(area(length1, width1), area(length2, width2); cout The sum of the two rectangless area is : totalendl; else total = totalarea(area(length1, width1); cout The sum of
8、 the rectangles area is : totalendl; int area(int a,int b) return (a*b); int totalarea(int a,int b) return (a + b); ,【3.6 函數(shù)重載】,例如:int fun(int, int); int fun(int); long fun(int, long); long fun(long);,例如:int fun1(int x, int y); long fun1(int x, int y);,例如:int fun(int a,int b); int fun(int x,int y);,
9、例 3.9,int add(int x, int y) cout In int add(int x, int y) endl; coutx+y=; return(x + y); /end of int add long add(long x, long y) cout In long add(long x, long y) endl; cout x + y =; return(x + y); /end of long add float add(float x, float y) cout In float add(float x, float y) endl; cout x + y =; r
10、eturn (x + y); /end of float add double add(double x, double y) cout In double add(double x, double y) endl; coutx+y=; return (x + y); /end of double add,例如:int fun(int a,int b=0); int fun(int a);,【 3.7 使用C+系統(tǒng)函數(shù)】,例 3.10, x = (x1 + x2) / 2.0; if (fabs(x + sqrt(x) - 2.8) 0) x2 = x; else x1 = x; ,【 3.8
11、 作用域】,【 3.8.1 作用域分類】,例如: double area(double x);,例如: void fun() int a; /a的作用域起始處 cina; if(a0) int b; / b的作用域起始處 . / b的作用域結(jié)束處 / a的作用域結(jié)束處,if語句,可以在語句中進(jìn)行條件測試的表達(dá)式內(nèi)聲明標(biāo)識符。,例如: if(int i=f() /標(biāo)識符i的作用域起始處 i=i*2; else i=100; /標(biāo)識符i的作用域結(jié)束處 couti; /錯誤,標(biāo)識符i在其作用域外不可見,例如: for(int i=0;i5;) /標(biāo)識符i的作用域起始處 i+; /標(biāo)識符i的作用域結(jié)束處 couti; /錯誤,標(biāo)識符i在其作用域外不可見,3. 函數(shù)作用域,例如:void fun() double a; int a; .; ,例如:void fun() int a; int a; .; ,在不同作用域內(nèi),允許聲明同名標(biāo)識符。,例如:void fun() double a(3); .; int
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024-2025學(xué)年廣東省深圳市高一上學(xué)期第一次月考英語試題(解析版)
- 2023-2024學(xué)年遼寧省名校聯(lián)盟高二下學(xué)期6月份聯(lián)合考試地理試卷(解析版)
- 單元檢測十 國家制度與社會治理
- 2024年中高壓齒輪泵項目發(fā)展計劃
- 2024年甲基丙烯酸甲酯項目合作計劃書
- 2024年第三方軟件項目發(fā)展計劃
- 2024年吡唑啉酮項目發(fā)展計劃
- 2024年金剛石觸媒材料項目發(fā)展計劃
- 2024年CO2激光心肌打孔機項目合作計劃書
- 2024年電子式金屬、非金屬試驗機項目發(fā)展計劃
- 工業(yè)機器人系統(tǒng)操作員理論考核題庫及答案
- 閃蒸罐計算完整版本
- 鄉(xiāng)村振興農(nóng)村設(shè)計案例分析報告
- 食用菌工廠化菌包生產(chǎn)線項目商業(yè)策劃書
- 《化工設(shè)備檢維修實訓(xùn)》課程標(biāo)準(zhǔn)(煤炭清潔利用技術(shù))
- 飲品類行業(yè)分析
- 山西省忻州原平市2023-2024學(xué)年七年級上學(xué)期期中語文試題
- 二年級上冊口算練習(xí)1000道
- 2024年廣東新華發(fā)行集團股份有限公司招聘筆試參考題庫含答案解析
- 稅務(wù)合規(guī)培訓(xùn)
- 企業(yè)號運營方案
評論
0/150
提交評論