




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Chapter 6Modularity Using Functions: Part IFunction and Parameter DeclarationsA function that is called into action by its reference in another function is a called function(被調(diào)用函數(shù)被調(diào)用函數(shù):一個(gè)函數(shù)通過被其他函數(shù)引用而起作用一個(gè)函數(shù)通過被其他函數(shù)引用而起作用)A function that calls another function is referred to as the calling function(調(diào)用
2、函數(shù)調(diào)用函數(shù))Function and Parameter Declarations調(diào)用和傳遞數(shù)據(jù)到函數(shù)調(diào)用和傳遞數(shù)據(jù)到函數(shù)Function and Parameter DeclarationsFunction Prototypes(函數(shù)原型函數(shù)原型)The declaration statement for a function is referred to as a function prototype (一個(gè)函數(shù)的聲明語句稱為函數(shù)原型一個(gè)函數(shù)的聲明語句稱為函數(shù)原型) Declares the data type of the value that will be directly ret
3、urned by the function(函數(shù)原型聲明將被這個(gè)函數(shù)直接返回的數(shù)值的數(shù)函數(shù)原型聲明將被這個(gè)函數(shù)直接返回的數(shù)值的數(shù)據(jù)類型據(jù)類型) Declares the data type of the values that need to be transmitted to the called function when it is invoked(在它被調(diào)用時(shí)需要傳遞給被在它被調(diào)用時(shí)需要傳遞給被調(diào)用函數(shù)的數(shù)值的數(shù)據(jù)類型調(diào)用函數(shù)的數(shù)值的數(shù)據(jù)類型) returnDataType functionName (argument data types);Calling a FunctionArg
4、uments(參數(shù)參數(shù)): items enclosed in parentheses in a function call statement(函數(shù)調(diào)用語句中被圓括號(hào)附上的條目)(函數(shù)調(diào)用語句中被圓括號(hào)附上的條目) Other terms used for arguments are actual arguments(實(shí)際參數(shù)實(shí)際參數(shù))and actual parameters(實(shí)參實(shí)參)Pass by value(按值傳遞按值傳遞): when a function receives copies of the values in each argument and must determ
5、ine where to store them before it does anything else(函數(shù)只是接受這些變量的數(shù)值復(fù)制品,并且在它做其他任函數(shù)只是接受這些變量的數(shù)值復(fù)制品,并且在它做其他任何事情之前必須確定在哪里存儲(chǔ)這些數(shù)值。何事情之前必須確定在哪里存儲(chǔ)這些數(shù)值。) Also referred to as call by value(數(shù)值調(diào)用數(shù)值調(diào)用)Calling a FunctionCalling a FunctionFunction Header LineFunction header(函數(shù)首部函數(shù)首部) : identifies the data type of th
6、e return value, provides the function with a name, and specifies the number, order, and type of values expected by the functionFunction body(函數(shù)體函數(shù)體): operates on the passed data and returns, at most, one valueThe argument names in the header line are known as parameters(參參數(shù)數(shù))or formal parameters(形參形
7、參)and formal arguments(形式參數(shù)形式參數(shù))Function Header LineFunction Header LineSome programmers prefer to put all called functions at the top of a program and make main() the last function listedEach C function is a separate and independent entity with its own parameters and variables(每個(gè)每個(gè)C 語言的函數(shù)都是分開的和獨(dú)立的,
8、有它語言的函數(shù)都是分開的和獨(dú)立的,有它們自己的參數(shù)和變量。們自己的參數(shù)和變量。) Nested functions are not permitted(決不允許把一個(gè)函數(shù)嵌套在另一決不允許把一個(gè)函數(shù)嵌套在另一個(gè)函數(shù)里面。個(gè)函數(shù)里面。 )Function Header LineEnds with a semicolonDoes not end with a semicolonFunction Header LineBasic (good) programming structure:preprocessor directivessymbolic constantsfunction prototy
9、pes can be placed hereint main() function prototypes can be placed here variable declarations; other executable statements; return value;Placement of Statements(語句的放置語句的放置)Returning a ValueFrom its side of the return transaction, the called function must provide: Data type of the returned value, whi
10、ch is specified in the functions header line Actual value being returned, which is specified by a return statementReturning a ValueReturning a ValueTo return a value, use a return statement return (expression); /or, return expression; The expression is evaluated first; its value is then automaticall
11、y converted to the return values data type as specified in the functions header line before being sent back to the calling function (表達(dá)式首先被計(jì)算。然后,表達(dá)式的數(shù)值在被送回到調(diào)用函數(shù)之前表達(dá)式首先被計(jì)算。然后,表達(dá)式的數(shù)值在被送回到調(diào)用函數(shù)之前自動(dòng)地轉(zhuǎn)換為在函數(shù)首部行中定義的返回值的數(shù)據(jù)類型。自動(dòng)地轉(zhuǎn)換為在函數(shù)首部行中定義的返回值的數(shù)據(jù)類型。)Returning a ValueFailure to exactly match the return value
12、 with the functions declared data type can lead to undesired results Return value is converted to the data type declared in the functions header line(返回的數(shù)值總是被轉(zhuǎn)換為函數(shù)首部行中聲明的數(shù)據(jù)類(返回的數(shù)值總是被轉(zhuǎn)換為函數(shù)首部行中聲明的數(shù)據(jù)類型)型)Returning a ValueStandard Library FunctionsThe standard library consists of 15 header filesBefore u
13、sing these functions, you must know The name of each available function The arguments required by each function The data type of the result (if any) returned by each function A description of what each function does How to include the library containing the desired functionT#include Mathematical Lib
14、rary FunctionsMathematical Library FunctionsThe rand() FunctionsRandom numbers(隨機(jī)數(shù)隨機(jī)數(shù))are a series of numbers whose order cannot be predicted(次序不能被預(yù)知的一系列數(shù)字)(次序不能被預(yù)知的一系列數(shù)字)All C compilers provide a function for creating random numbers: rand() ,defined in the stdlib.h header file rand() produces rando
15、m numbers in the range 0 rand() RAND_MAX(RAND_MAX是一個(gè)取決于編譯器的在頭文件中被定義的符號(hào)常量。)是一個(gè)取決于編譯器的在頭文件中被定義的符號(hào)常量。)Scaling(比例縮放比例縮放)The method for adjusting the random numbers produced by a random-number generator to reside within a specified range is called scaling(調(diào)調(diào)整由隨機(jī)數(shù)發(fā)生器產(chǎn)生的隨機(jī)數(shù)在一個(gè)指定的范圍內(nèi)的方法叫比例縮整由隨機(jī)數(shù)發(fā)生器產(chǎn)生的隨機(jī)數(shù)在一個(gè)
16、指定的范圍內(nèi)的方法叫比例縮放放)To scale a random number as an integer value between 1 and N:1 + (int)rand() % NTo produce a random integer between the numbers a and b:a + (int)(rand() % (b - a + 1)Input/Output Library Functionsgetchar() can be used for single character inputinchar=getchar();scanf(“%c”,&inchar);putchar() expects a single character argument
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 購銷石材合同(4篇)
- 機(jī)械工程自動(dòng)化生產(chǎn)線試題庫
- 幸福中國(guó)演講稿(8篇)
- 歷史文獻(xiàn)研究試題集
- 合同協(xié)議書怎么簽字合法
- 軟件測(cè)試基礎(chǔ)知識(shí)梳理試題及答案
- 嵌入式開發(fā)團(tuán)隊(duì)的角色分配試題及答案
- 監(jiān)理師考試資料如何選擇試題及答案2025年
- 三模物理試題及答案
- 做肉餅的試卷試題及答案
- 2025年關(guān)于銀行業(yè)數(shù)字化轉(zhuǎn)型的關(guān)鍵與思考范文
- GB/T 28583-2025供電服務(wù)規(guī)范
- 阿爾茨海默病疾病修飾治療專家共識(shí)(2025版)解讀
- 設(shè)備故障應(yīng)急維修預(yù)案
- (3篇)2025年春季形勢(shì)與政策大作業(yè):怎樣正確理解全過程人民民主的歷史邏輯、實(shí)踐邏輯、理論邏輯?與專題測(cè)驗(yàn)(1-5)附答案
- 吉林2025年生態(tài)環(huán)境部松遼流域生態(tài)環(huán)境監(jiān)督管理局生態(tài)環(huán)境監(jiān)測(cè)與科學(xué)研究中心招聘筆試歷年參考題庫附帶答案詳解
- 四川?。拼笥嶏w大數(shù)據(jù))2025屆高三第二次教學(xué)質(zhì)量聯(lián)合測(cè)評(píng)物理試題及答案
- TSG Z7002-2022特種設(shè)備檢測(cè)機(jī)構(gòu)核準(zhǔn)規(guī)則
- 鍋爐檢修作業(yè)安全保障方案
- 2025-2030中國(guó)三醋酸纖維素膜行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- 三基三嚴(yán)培訓(xùn)課件
評(píng)論
0/150
提交評(píng)論