📄 6.5.2.htm
字号:
<html>
<head>
<title>编译原理</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link type="text/css" rel="stylesheet" href="../css/specification.css">
</head>
<body>
<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.1.htm'"></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.3.htm'"></img></td>
</tr>
</table>
<br><br>
<font class="title2"><b>6.5.2 引用调用</b></font>
<table><tr><td>    </td>
<td class="content">
<P>
引用调用(call-by-reference)也称传地址调用(call-by-address)或传位置调用(call-by-location),是指把实在参数的地址传递给相应的形式参数,或者说调用过程把一个指向实在参数的存储地址的指针传递给被调用过程的相应的形式参数。实现如下:
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
1.如果一个实在参数是一个名字或者是一个具有左-值的表达式,那么这个左-值本身被传递。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
2.然而,如果实在参数是一个表达式,如a+b或2等,它们无左-值,那么计算出表达式的值并放人新的存储单元,然后将此单元的地址传递过去。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
在目标代码中,在被调用过程中对形式参数的一次引用就成为对传递给被调用过程的指针的一个间接引用。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
(1) <b><font color="#0000FF">program</font> </b>reference(input,output); <br>
(2) <b><font color="#0000FF">var</font></b> a,b: integer; <br>
(3) <b><font color="#0000FF">procedure</font></b> swap(var x,y: integer); <br>
(4) <b> <font color="#0000FF">var</font></b> temp: integer; <br>
(5) <b> <font color="#0000FF">begin</font></b> <br>
(6) temp:=x;<br>
(7) x:=y;<br>
(8) y:=temp <br>
(9) <b> <font color="#0000FF">end</font></b>; <br>
(10) <b> <font color="#0000FF">begin</font></b> <br>
(11) a:=1;b:=2; <br>
(12) swap(a,b); <br>
(13) writeln('a=',a); writeln('b=', b)<br>
(14) <b> <font color="#0000FF">end</font></b>. </p>
<p> <b>图6.24</b> 过程swap的形式参数x和y为变量参数
<p>
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
<b>例6.6</b> 考虑图6.24中的过程swap。其中形式参数x和y为变量参数。一个带有实在参数i和a[i]的对swap的调用,即swap(i,a[i]),与下列步骤具有同样的作用:
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
1.把i和a[i]的地址(即左-值)复制到被调用过程的活动记录中,或说复制到分别与x和y相对应的存储单元argl和arg2中。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
2.把temp的内容置为arg1所指向的单元中存放的内容。这一步对应于过程swap的定义中的第(6)行的temp:=x。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
3.把由arg1所指向的单元的内容置为arg2所指向的单元的值。这一步对应于swap内的第(7)行上的x:=y。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
4.把由arg2所指向的单元的内容置为temp的值。这一步相当于y=temp。
</p>
</td></tr></table>
<table><tr><td>    </td>
<td class="content">
<P>
有相当多的语言使用引用调用。在Pascal中变量参数就是用这种方法传递的。
</p>
</td></tr></table>
<br>
<table align=right width=300>
<tr>
<td><img src="../images/previous.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.1.htm'"></img></td>
<td><img src="../images/next.gif" onmouseover="javascript:style.cursor='hand'" onclick="vbscript:window.location.href='6.5.3.htm'"></img></td>
</tr>
</table>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -