📄 page_543.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_543</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_542.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_543</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_544.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 543</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="Courier New, Courier, Mono New, Courier, Mono" size="2">int聽Factorial(聽int聽);<br />float聽CargoMoment(聽int聽);</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">C++ allows a function return value to be of <i>any</i> data typebuilt-in or userdefinedexcept an array (a data type we introduce in the next chapter).</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">In the last section, we wrote a Switch statement to convert a pair of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">char</font><font face="Times New Roman, Times, Serif" size="3"> values into a value of type </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Animals</font><font face="Times New Roman, Times, Serif" size="3">. Let's write a value-returning function that performs this task. Notice how the function heading declares the data type of the return value to be </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">Animals</font><font face="Times New Roman, Times, Serif" size="3">.</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">Animals聽CharToAnimal(聽/*聽in聽*/聽char聽ch1,<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽/*聽in聽*/聽char聽ch2聽)<br />{<br />聽聽聽聽switch聽(ch1)<br />聽聽聽聽{<br />聽聽聽聽聽聽聽聽case聽R聽:聽if聽(ch2聽==聽o)<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽return聽RODENT;<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽return聽REPTILE;<br />聽聽聽聽聽聽聽聽case聽C聽:聽return聽CAT;<br />聽聽聽聽聽聽聽聽case聽D聽:聽return聽DOG;<br />聽聽聽聽聽聽聽聽case聽B聽:聽if聽(ch2聽==聽i)<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽return聽BIRD;<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽return聽BOVINE;<br />聽聽聽聽聽聽聽聽case聽H聽:聽return聽HORSE;<br />聽聽聽聽聽聽聽聽default聽:聽聽return聽SHEEP;<br />聽聽聽聽}<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">In this function, why didn't we include a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">break</font><font face="Times New Roman, Times, Serif" size="3"> statement after each case alternative? Because when each alternative executes a </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">return</font><font face="Times New Roman, Times, Serif" size="3"> statement, control immediately exits the function. It's not possible for control to fall through to the next alternative.</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">Here is a sample of code that calls the </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">CharToAnimal</font><font face="Times New Roman, Times, Serif" size="3"> 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="Courier New, Courier, Mono New, Courier, Mono" size="2">enum聽Animals聽{RODENT,聽CAT,聽DOG,聽BIRD,聽REPTILE,聽HORSE,聽BOVINE,聽SHEEP};<br /><br />Animals聽CharToAnimal(聽char,聽char聽);<br />聽聽.<br />聽聽.<br />聽聽.<br />int聽main()<br />{<br />聽聽聽聽Animals聽inPatient;<br />聽聽聽聽Animals聽outPatient;<br />聽聽聽聽char聽聽聽聽char1;<br /></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_542.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_543</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_544.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -