📄 lib0029.html
字号:
<span style="background-color:d9d9d9">SUBROUTINE GETMAX(ARR,NELM,MX)</span>
<span style="background-color:d9d9d9">INTEGER ARR(NELM)</span>
<span style="background-color:d9d9d9">INTEGER MX</span>
<span style="background-color:d9d9d9">MX=ARR(1)</span>
<span style="background-color:d9d9d9">DO 20,INDEX =2,NELM</span>
<span style="background-color:d9d9d9">IF(ARR(INDEX)>MX) THEN</span>
<span style="background-color:d9d9d9">MX = ARR(INDEX)</span>
<span style="background-color:d9d9d9">END IF</span>
<span style="background-color:d9d9d9">20 CONTINUE</span>
<span style="background-color:d9d9d9">END</span>
</pre>
</div>
<p class="para">If you run this program, you will see:</p>
<div class="informalexample">
<pre class="literallayout">
val= 10.1999998
max= 26
</pre>
</div>
<p class="para">As you can see, F77 provides much better encapsulation than COBOL 85. Each procedure is capable of declaring its own <a name="376"></a><a name="IDX-180"></a>arguments, local variables, and return values. By placing these variables where they are relevant, instead of in a global data section, the code is much easier to read and reuse.</p>
<blockquote class="blockquote">
<div class="qandaset">
<table border="0" cellpadding="0">
<tr class="qandaentry">
<td class="td" valign="top" width="2%">
<p class="first-para">
<a name="LiB22"><b>1. </b></a>
</p>
</td><td class="td" valign="top" width="90%">
<p class="first-para">How does FORTRAN support procedure arguments and local variables without a stack?</p>
</td><td class="td" valign="top" width="8%">
<p class="first-para">
<a href="#LiB21"><img src="images/question.gif" height="24" width="24" alt="In F77, each routine has its own private stash of static memory that serves as a storage space for local variables and arguments. This precludes F77 from implementing recursion, but it does allow an F77 program to utilize a more sophisticated memory model than COBOL 85. An example F77 memory model is displayed in Figure 3.15 . " border="0"></a>
</p>
</td>
</tr>
</table>
<p class="bold">Answers</p>
<table border="0" cellpadding="0">
<tr class="qandaentry-answer">
<td class="td" valign="top" width="2%">
<p class="first-para">
<a class="internaljump" name="answer.nr-qandaentry.B48FD990-0554-489F-8771-97F9FA2758D8" href="#LiB22"><b>1.</b></a> </p>
</td><td class="td" valign="top" width="90%">
<p class="first-para">In F77, each routine has its own private stash of static memory that serves as a storage space for local variables and arguments. This precludes F77 from implementing recursion, but it does allow an F77 program to utilize a more sophisticated memory model than COBOL 85. An example F77 memory model is displayed in <a class="internaljump" href="#ch03fig15">Figure 3.15</a>.<div class="figure">
<a name="377"></a><a name="ch03fig15"></a><span class="figuremediaobject"><img src="images/fig208_01.jpg" height="259" width="329" alt="" border="0"></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 3.15</span></span>
</div>
</p>
</td>
</tr>
</table>
</div>
</blockquote>
<p class="para">One feature that this per-procedure static memory space supports is the <span class="fixed">SAVE</span> statement. The <span class="fixed">SAVE</span> statement allows the local variables of a procedure to sustain their values between function calls. If FORTRAN used an activation record for local variables, it wouldn't be possible to implement <span class="fixed">SAVE</span>.</p>
<p class="para">Here is a short example demonstrating the <span class="fixed">SAVE</span> statement:</p>
<div class="informalexample">
<pre class="literallayout">
<span style="background-color:d9d9d9">PROGRAM RUNSUM</span>
<span style="background-color:d9d9d9">WRITE(*,*) SIGMA(3)</span>
<span style="background-color:d9d9d9">WRITE(*,*) SIGMA(5)</span>
<span style="background-color:d9d9d9">WRITE(*,*) SIGMA(2)</span>
<span style="background-color:d9d9d9">WRITE(*,*) SIGMA(7)</span>
<span style="background-color:d9d9d9">END</span>
<span style="background-color:d9d9d9">*----------------------------------------------------</span>
<span style="background-color:d9d9d9">FUNCTION SIGMA(VAL)</span><a name="378"></a><a name="IDX-181"></a>
<span style="background-color:d9d9d9">INTEGER VAL</span>
<span style="background-color:d9d9d9">INTEGER SUM</span>
<span style="background-color:d9d9d9">SAVE SUM</span>
<span style="background-color:d9d9d9">SUM=SUM+VAL</span>
<span style="background-color:d9d9d9">SIGMA = SUM</span>
<span style="background-color:d9d9d9">END</span>
</pre>
</div>
<p class="para">When the previous program is run, the following output is displayed:</p>
<div class="informalexample">
<pre class="literallayout">
3.
8.
10.
17.
</pre>
</div>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Case Study: </span>Pascal</span><a name="379"></a><a></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<p class="first-para">In 1971 Niklaus Wirth presented the world with his specification of a structured language named after a French mathematician who lived during the 17th century. Pascal was inspired heavily by a programming language called ALGOL. This seems only natural when you consider that Wirth was part of the group that originally created ALGOL. During the 1960s, FORTRAN had supplanted ALGOL as the mathematical programming language of choice. As a result, the designers of ALGOL were looking for ways to extend the language.</p>
<p class="para">Pascal supports heavy use of the stack and, unlike F77, allows function calls to be recursive. Pascal also provides manual access to the heap via the <span class="fixed">NEW</span> and <span class="fixed">DISPOSE</span> statements. Pascal allows global variables to be defined, which are stored in a static data segment. Pascal is the first language that we have examined that uses the stack, the heap, and a data section.</p>
<p class="para">Wirth admits, however, that Pascal is really a toy language that is intended for educational purposes. The language has a limited set of features, and this handicap is compounded by the fact that the Pascal compiler enforces a rigid set of syntax rules. Pascal is not a suitable language for developing large projects and has been called a bondage-discipline language by some engineers. Wirth ended up moving on to invent other languages like Modula and Oberon. Borland, which marketed a very successful Pascal compiler in the 1980s, currently sells an object-oriented variation of Pascal called Delphi.</p>
<p class="para">
<table border="0" cellspacing="0" cellpadding="0" class="note">
<tr>
<td valign="top" class="admon-check"></td><td valign="top" class="admon-title">Note </td><td valign="top" class="admon-body">
<p class="first-para">According to the <I>Hacker's Dictionary, a bondage-discipline programming language</I> is one that forces the programmer to abide by a strict set of syntax rules. The term is used derisively by programmers who feel that a language's syntax rules have their origins in the language designer's world view rather than pragmatic inspiration.</p>
</td>
</tr>
</table>
</p>
<a name="380"></a><a name="IDX-182"></a>
<p class="para">A Pascal program lies entirely within the boundaries of its <span class="fixed">PROGRAM</span> procedure. Inside of the <span class="fixed">PROGRAM</span> routine are a number of functions that may be arbitrarily nested. This nesting is particularly annoying to a C programmer like me. A <span class="fixed">FUNCTION</span> is a routine that returns a single value via its identifier, and a <span class="fixed">PROCEDURE</span> is a function that does not. As with COBOL, procedural code is always prefixed by variable declarations and definitions.</p>
<p class="para">Consider the following program:</p>
<div class="informalexample">
<pre class="literallayout">
<span style="background-color:d9d9d9">program main;</span>
<span style="background-color:d9d9d9">var</span>
<span style="background-color:d9d9d9">value:integer;</span>
<span style="background-color:d9d9d9">procedure callNested;</span>
<span style="background-color:d9d9d9">function nested1:integer;</span>
<span style="background-color:d9d9d9">function nested2:integer;</span>
<span style="background-color:d9d9d9">begin</span>
<span style="background-color:d9d9d9">writeln('inside nested2()');</span>
<span style="background-color:d9d9d9">nested2 := value+1;</span>
<span style="background-color:d9d9d9">end;</span>
<span style="background-color:d9d9d9">begin</span>
<span style="background-color:d9d9d9">writeln('inside nested1()');</span>
<span style="background-color:d9d9d9">nested1 := nested2+1;</span>
<span style="background-color:d9d9d9">end;</span>
<span style="background-color:d9d9d9">begin</span>
<span style="background-color:d9d9d9">writeln('inside callNested()');</span>
<span style="background-color:d9d9d9">writeln('value= ',nested1);</span>
<span style="background-color:d9d9d9">end;</span>
<span style="background-color:d9d9d9">begin</span>
<span style="background-color:d9d9d9">value:=5;</span>
<span style="background-color:d9d9d9">callNested;</span>
<span style="background-color:d9d9d9">end.</span>
</pre>
</div>
<p class="para">When run, this program will generate the following output:</p>
<div class="informalexample">
<pre class="literallayout">
inside callNested()
value= inside nested1()
inside nested2()
7
</pre>
</div>
<p class="para">Here is another brief example that demonstrates how Pascal can use different memory components:</p>
<div class="informalexample">
<pre class="literallayout">
<span style="background-color:d9d9d9">program ptour;</span>
<span style="background-color:d9d9d9">const</span>
<span style="background-color:d9d9d9">size=6;</span>
<span style="background-color:d9d9d9">type</span>
<span style="background-color:d9d9d9">intpointer=^integer;</span>
<span style="background-color:d9d9d9">var</span>
<span style="background-color:d9d9d9">iptr:intPointer;</span><a name="381"></a><a name="IDX-183"></a>
<span style="background-color:d9d9d9">index:integer;</span>
<span style="background-color:d9d9d9">function factorial(arg:integer):integer;</span>
<span style="background-color:d9d9d9">begin</span>
<span style="background-color:d9d9d9">if arg>1 then</span>
<span style="background-color:d9d9d9">factorial := arg * factorial(arg-1)</span>
<span style="background-color:d9d9d9">else</span>
<span style="background-color:d9d9d9">factorial :=1;</span>
<span style="background-color:d9d9d9">end;</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -