C語言英文ch06_第1頁
C語言英文ch06_第2頁
C語言英文ch06_第3頁
C語言英文ch06_第4頁
C語言英文ch06_第5頁
已閱讀5頁,還剩21頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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(被調用函數被調用函數:一個函數通過被其他函數引用而起作用一個函數通過被其他函數引用而起作用)A function that calls another function is referred to as the calling function(調用

2、函數調用函數)Function and Parameter Declarations調用和傳遞數據到函數調用和傳遞數據到函數Function and Parameter DeclarationsFunction Prototypes(函數原型函數原型)The declaration statement for a function is referred to as a function prototype (一個函數的聲明語句稱為函數原型一個函數的聲明語句稱為函數原型) Declares the data type of the value that will be directly ret

3、urned by the function(函數原型聲明將被這個函數直接返回的數值的數函數原型聲明將被這個函數直接返回的數值的數據類型據類型) Declares the data type of the values that need to be transmitted to the called function when it is invoked(在它被調用時需要傳遞給被在它被調用時需要傳遞給被調用函數的數值的數據類型調用函數的數值的數據類型) returnDataType functionName (argument data types);Calling a FunctionArg

4、uments(參數參數): items enclosed in parentheses in a function call statement(函數調用語句中被圓括號附上的條目)(函數調用語句中被圓括號附上的條目) Other terms used for arguments are actual arguments(實際參數實際參數)and actual parameters(實參實參)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(函數只是接受這些變量的數值復制品,并且在它做其他任函數只是接受這些變量的數值復制品,并且在它做其他任何事情之前必須確定在哪里存儲這些數值。何事情之前必須確定在哪里存儲這些數值。) Also referred to as call by value(數值調用數值調用)Calling a FunctionCalling a FunctionFunction Header LineFunction header(函數首部函數首部) : 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(函數體函數體): operates on the passed data and returns, at most, one valueThe argument names in the header line are known as parameters(參參數數)or formal parameters(形參形

7、參)and formal arguments(形式參數形式參數)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(每個每個C 語言的函數都是分開的和獨立的,

8、有它語言的函數都是分開的和獨立的,有它們自己的參數和變量。們自己的參數和變量。) Nested functions are not permitted(決不允許把一個函數嵌套在另一決不允許把一個函數嵌套在另一個函數里面。個函數里面。 )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 (表達式首先被計算。然后,表達式的數值在被送回到調用函數之前表達式首先被計算。然后,表達式的數值在被送回到調用函數之前自動地轉換為在函數首部行中定義的返回值的數據類型。自動地轉換為在函數首部行中定義的返回值的數據類型。)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(返回的數值總是被轉換為函數首部行中聲明的數據類(返回的數值總是被轉換為函數首部行中聲明的數據類型)型)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(隨機數隨機數)are a series of numbers whose order cannot be predicted(次序不能被預知的一系列數字)(次序不能被預知的一系列數字)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是一個取決于編譯器的在頭文件中被定義的符號常量。)是一個取決于編譯器的在頭文件中被定義的符號常量。)Scaling(比例縮放比例縮放)The method for adjusting the random numbers produced by a random-number generator to reside within a specified range is called scaling(調調整由隨機數發生器產生的隨機數在一個指定的范圍內的方法叫比例縮整由隨機數發生器產生的隨機數在一個

16、指定的范圍內的方法叫比例縮放放)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等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論