📄 page_357.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_357</title> <link rel="stylesheet" href="reset.css" type="text/css" media="all"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <table summary="top nav" border="0" width="100%"> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_356.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_357</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_358.html">next page ></a></td> </tr> <tr> <td align="left" colspan="3" style="background: #ffffff; padding: 20px;"> <table border="0" width="100%" cellpadding="0"><tr><td align="center"> <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td> <td align="right"></td> </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 357</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">to the declaration of the formal parameter. (Remember that C++ comments come in two forms. The first, which we use most often, starts with two slashes and extends to the end of the line. The second form encloses a comment between </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">/*</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">*/</font><font face="Times New Roman, Times, Serif" size="3"> and allows us to embed a comment within a line of code.) Here is the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">PrintAverage</font><font face="Times New Roman, Times, Serif" size="3"> function with comments added to the formal parameter declarations:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">void聽PrintAverage(聽/*聽in聽*/聽float聽sum,<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽/*聽in聽*/聽int聽count聽)<br />//聽Precondition:<br />//聽聽聽聽聽sum聽is聽assigned聽&&聽聽聽count聽>聽0<br />//聽Postcondition:<br />//聽聽聽聽聽The聽average聽sum/count聽has聽been聽output聽on聽one聽line<br />{<br />聽聽聽聽cout聽<聽Average聽is聽聽<聽sum聽/聽float(count)聽<聽endl;<br />}</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Pass-by-value is appropriate for each parameter that is incoming only. As you can see in the function body, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">PrintAverage</font><font face="Times New Roman, Times, Serif" size="3"> does not modify the values of the parameters </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">sum</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">count</font><font face="Times New Roman, Times, Serif" size="3">. It merely uses their current values. The direction of data flow is one-wayinto the function.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">The data flow for an outgoing parameter is one-wayout of the function. The function produces a new value for the parameter without using the old value in any way. The comment </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">/*</font><font face="Times New Roman, Times, Serif" size="3"> out </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">*/</font><font face="Times New Roman, Times, Serif" size="3"> identifies an outgoing parameter. Here we've added comments to the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Get2Ints</font><font face="Times New Roman, Times, Serif" size="3"> function heading:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">void聽Get2Ints(聽聽/*聽out聽*/聽int&聽int1,<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽/*聽out聽*/聽int&聽int2聽聽)</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Pass-by-reference must be used for an outgoing parameter. If you look back at the body of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Get2Ints</font><font face="Times New Roman, Times, Serif" size="3">, you'll see that the function stores new values into the two variables (by means of the input statement), replacing whatever values they originally contained.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">Finally, the data flow for an incoming/outgoing parameter is two-way into and out of the function. The function uses the old value and also produces a new value for the parameter. We use </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">/*聽inout聽*/</font><font face="Times New Roman, Times, Serif" size="3"> to document this two-way direction of data flow. Here is an example of a function that uses two parameters, one of them incoming only and the other one incoming/outgoing:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>聽 </td> </tr> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_356.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_357</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_358.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -