




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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ù)通過(guò)被其他函數(shù)引用而起作用一個(gè)函數(shù)通過(guò)被其他函數(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ù)的聲明語(yǔ)句稱為函數(shù)原型一個(gè)函數(shù)的聲明語(yǔ)句稱為函數(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ù)類(lèi)型據(jù)類(lèi)型) 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ù)類(lèi)型調(diào)用函數(shù)的數(shù)值的數(shù)據(jù)類(lèi)型) returnDataType functionName (argument data types);Calling a FunctionArg
4、uments(參數(shù)參數(shù)): items enclosed in parentheses in a function call statement(函數(shù)調(diào)用語(yǔ)句中被圓括號(hào)附上的條目)(函數(shù)調(diào)用語(yǔ)句中被圓括號(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 語(yǔ)言的函數(shù)都是分開(kāi)的和獨(dú)立的,
8、有它語(yǔ)言的函數(shù)都是分開(kāi)的和獨(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(語(yǔ)句的放置語(yǔ)句的放置)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ù)類(lèi)型。自動(dòng)地轉(zhuǎn)換為在函數(shù)首部行中定義的返回值的數(shù)據(jù)類(lèi)型。)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ù)類(lèi)(返回的數(shù)值總是被轉(zhuǎn)換為函數(shù)首部行中聲明的數(shù)據(jù)類(lèi)型)型)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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 專(zhuān)注中央廚房采購(gòu)合同標(biāo)準(zhǔn)文本
- 鄉(xiāng)下房子轉(zhuǎn)讓合同范例
- 買(mǎi)賣(mài)水果雙方合同范例
- 黨校培訓(xùn)住宿合同標(biāo)準(zhǔn)文本
- web外包合同標(biāo)準(zhǔn)文本
- 冷庫(kù)貯藏蘋(píng)果合同標(biāo)準(zhǔn)文本
- 辦公區(qū)域轉(zhuǎn)讓合同標(biāo)準(zhǔn)文本
- 以合伙名義招工合同標(biāo)準(zhǔn)文本
- 016勞務(wù)派遣合同標(biāo)準(zhǔn)文本
- 勞動(dòng)合同書(shū)模板
- 尪痹的護(hù)理查房
- 艾瑞咨詢-2024年中國(guó)數(shù)字世界平臺(tái)創(chuàng)新趨勢(shì)發(fā)展研究報(bào)告:以實(shí)為本以虛強(qiáng)實(shí)
- 精神科理論知識(shí)考核試題題庫(kù)及答案
- 99D102-1 6~10kV鐵橫擔(dān)架空絕緣線路安裝
- 渤中19-6凝析氣田試驗(yàn)區(qū)開(kāi)發(fā)項(xiàng)目(第二階段)環(huán)評(píng)報(bào)告
- 中醫(yī)養(yǎng)生之藥膳食療考試試題
- 3D打印人體器官
- 矩形的性質(zhì)(公開(kāi)課)
- 日語(yǔ)履歷書(shū)模板
- 石材等各類(lèi)幕墻材料計(jì)算規(guī)則
- 新地球觀地球系統(tǒng)科學(xué)課件
評(píng)論
0/150
提交評(píng)論