📄 right2-2-2.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 2 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">
<span style="background-color: #CCFF99">第 2 章</span><span style="background-color: #CCFF99">
</span>><span style="background-color: #CCFF99"> 第 2 节 </span>><span style="background-color: #CCFF99">
2.2 函数(2)</span></p>
<hr color="#008000" size="1">
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2">3.
</font><font FACE="??ì?,SimSun" LANG="ZH-CN"><font size="2">函数的值</font></p>
</font><font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">对函数的值(或称函数返回值)有以下一些说明:</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">①</font>
<font FACE="??ì?,SimSun" LANG="ZH-CN">函数的值只能通过</font>return<font FACE="??ì?,SimSun" LANG="ZH-CN">语句返回主调函数。</font>return<font FACE="??ì?,SimSun" LANG="ZH-CN">语句的语法格式为:</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
return <font FACE="??ì?,SimSun" LANG="ZH-CN">表达式</font>;</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">或者为:</font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">
return (<font FACE="??ì?,SimSun" LANG="ZH-CN">表达式</font>);</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">该语句的功能是计算表达式的值,并返回给主调函数。在函数中允许有多个</font>return<font FACE="??ì?,SimSun" LANG="ZH-CN">语句,但每次调用只能有一个</font>return<font FACE="??ì?,SimSun" LANG="ZH-CN">语句被执行,因此只能返回一个函数值。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">②</font>
<font FACE="??ì?,SimSun" LANG="ZH-CN">函数值的类型和函数定义中函数的类型应保持一致。如果两者不一致,则以函数类型为准,自动进行类型转换。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">③</font>
<font FACE="??ì?,SimSun" LANG="ZH-CN">如函数值为整型,在函数定义时可以省去类型说明。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">④</font>
<font FACE="??ì?,SimSun" LANG="ZH-CN">不返回函数值的函数,可以明确定义为“空类型”,类型标识符为“</font>void<font FACE="??ì?,SimSun" LANG="ZH-CN">”。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2">4.
</font><font FACE="??ì?,SimSun" LANG="ZH-CN"><font size="2">函数的嵌套调用</font></p>
</font>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">C++<font FACE="??ì?,SimSun" LANG="ZH-CN">语言允许在一个函数的定义中出现对另一个函数的调用。这样就出现了函数的嵌套调用,即在被调函数中又调用其它函数。这与其它语言的子程序嵌套的情形是类似的。其关系如图所示。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 210; top: 388"><img border="0" src="right28.gif" width="331" height="139"></span></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
</font>
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="CENTER" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font face="??ì?,SimSun" lang="ZH-CN" size="2">该</font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">图表示了两层嵌套的情形。其执行过程是:执行</font>main<font FACE="??ì?,SimSun" LANG="ZH-CN">函数中调用</font>a<font FACE="??ì?,SimSun" LANG="ZH-CN">函数的语句时,即转去执行</font>a<font FACE="??ì?,SimSun" LANG="ZH-CN">函数;在</font>a<font FACE="??ì?,SimSun" LANG="ZH-CN">函数中调用</font>b<font FACE="??ì?,SimSun" LANG="ZH-CN">函数时,又转去执行</font>b<font FACE="??ì?,SimSun" LANG="ZH-CN">函数;</font>b<font FACE="??ì?,SimSun" LANG="ZH-CN">函数执行完毕返回</font>a<font FACE="??ì?,SimSun" LANG="ZH-CN">函数的断点继续执行,</font>a<font FACE="??ì?,SimSun" LANG="ZH-CN">函数执行完毕返回</font>main<font FACE="??ì?,SimSun" LANG="ZH-CN">函数的断点继续执行。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">【例</font>2-12<font FACE="??ì?,SimSun" LANG="ZH-CN">】计算</font>(1!)<sup>2</sup>
+ (2!)<sup>2</sup> + (3!)<sup>2</sup> + (4!)<sup>2</sup> + (5!)<sup>2</sup> <font FACE="??ì?,SimSun" LANG="ZH-CN">的值。</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">分析:本题可编写两个函数,一个是用来计算平方值的函数</font>f1<font FACE="??ì?,SimSun" LANG="ZH-CN">,另一个是用来计算阶乘值的函数</font>f2<font FACE="??ì?,SimSun" LANG="ZH-CN">。主函数先调</font>f1<font FACE="??ì?,SimSun" LANG="ZH-CN">,在</font>f1<font FACE="??ì?,SimSun" LANG="ZH-CN">中再调用</font>f2<font FACE="??ì?,SimSun" LANG="ZH-CN">计算其阶乘值,然后返回</font>f1<font FACE="??ì?,SimSun" LANG="ZH-CN">,由</font>f1<font FACE="??ì?,SimSun" LANG="ZH-CN">返回平方值,在循环程序中计算累加和。</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#include
"iostream.h"</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">long
f1(int p) <font color="#008000">/* <font FACE="??ì?,SimSun" LANG="ZH-CN">定义函数</font>f1
*/</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
long r;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
long f2(int); <font color="#008000">/* <font FACE="??ì?,SimSun" LANG="ZH-CN">声明函数</font>f2
*/</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
r=f2(p); /* <font FACE="??ì?,SimSun" LANG="ZH-CN">计算</font>p<font FACE="??ì?,SimSun" LANG="ZH-CN">的阶乘</font>
*/</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
return r*r; /* <font FACE="??ì?,SimSun" LANG="ZH-CN">返回阶乘的平方</font>
*/</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">long
f2(int q) /* <font FACE="??ì?,SimSun" LANG="ZH-CN">计算阶乘的函数</font>
*/</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
long c=1;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
int i;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
for(i=1;i<=q;i++)</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
c=c*i;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
return c;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
main( )</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
int i;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
long s=0;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
for (i=1;i<=5;i++)</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
s=s+f1(i);</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">
cout<<"\ns="<<s;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
</blockquote>
</blockquote>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 34; top: 1076"><a href="right2-2.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 495; top: 1079"><a href="right2-3.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -