📄 pasl1005.html
字号:
<HTML>
<head>
<LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 6</Title>
</head>
<body background="../tile01.jpg" >
<H1><center>What the Hell are Procedures and Functions</center></h1><p> </p><br>
<p>Hello ! Nice to meet you again ! How is the lessons ? It was a great quiz,
wasn't it ? Well, time's getting more difficult to pass through. I've got a very busy
semester, so that I may not even keep my promise in the Web........ Sorry !
But I keep moving on, don't worry !</p>
<p>Contents :</p><ol>
<li>Simple syntaxes</li>
<li>Scope</li>
<li>Local variables and global variables</li>
<li>Parameters (pass by value and pass by reference)</li>
<li>Ignored return values</li>
<li>Inter-procedure and/or functions call</li>
<li>Nested syntax</li>
<li>Recursive calls</li>
<li>Inter-referenced procedures/functions</li>
<li>Break down problems</li></ol>
<p>OK ! Let's start ! We know that Pascal is a structured language. It means
that the problems is divided into steps and subproblems and then is coded
in a structure. This division is made through procedures and functions.
Well, what the hell are procedures and functions ? It is like a subprogram
that has its own <tt>begin..end</tt>. It is useful when a part of program must be performed
more than once in scattered parts of the program. May be it is a bit
murky, but let's take a look on its syntax :</p>
<pre>
Procedure procname;
begin
:
{ commands }
:
:
end;
begin
end.
</pre><p>
This is the simplest form of a procedure. (Functions are discussed later)
As we can see that procedures are always placed ABOVE the main <tt>begin...end</tt>.
Example :</p><pre></pre>
<hr>
<pre>uses crt;
procedure pause;
begin
writeln('Press any key when ready ...');
readkey;
end;
begin
clrscr;
writeln('ABC');
pause; { Make a call to procedure pause }
writeln('Hey, let''s pause !');
pause; { Make a call to procedure pause }
writeln('Pause again !');
pause; { Make a call to procedure pause }
end.
</pre>
<hr><p>Run the previous example. Look at this example :<hr><pre>
uses crt;
begin
clrscr;
writeln('ABC');
writeln('Press any key when ready ...');
readkey;
writeln('Hey, let''s pause !');
writeln('Press any key when ready ...');
readkey;
writeln('Pause again !');
writeln('Press any key when ready ...');
readkey;
end.
</pre><hr><p>
It gives the same effect as the first example, isn't it ? Which is nicer ?
Writing silly same phrases or efficiently using procedures. If you are normal
guy, you would love the first example. It is clearer and cleaner. Easy
to read. Especially the lines to be replaced with procedures are very long.
Procedures, and functions too, make the codes healthier and easier to read.
It makes life easier in debugging the program, too.</p>
<p>Procedures could have its own variable, called local variable. The variable
is only known inside the procedure. For example :</p><hr><pre>
procedure foo;
var a : byte;
begin
a:=10;
writeln(a);
end;
begin { main begin...end block }
foo;
a:=15; { This will be an error, since a is only known inside foo }
end.
</pre><hr><p>
Global variable is a variable that is known throughout the program and its
procedures or functions. Example :</p><hr><pre>
var
b : byte;
procedure foo;
var a : byte;
begin
a:=10;
b:=15; { This is legal }
end;
begin { main begin...end block }
foo;
a:=15; { This is illegal }
b:=5; { This is perfectly legal }
end.
</pre><hr><p>
If a local variable is declared inside a procedure with the same name as
the global variable, the global variable is overrided.
For example :</p><hr><pre>
uses crt;
var
a : byte; { This is the global variable }
procedure foo;
var a : byte;
begin
a:=15;
writeln('foo is ',a);
end;
begin
a:=10;
writeln(a);
foo;
writeln(a);
end.
</pre><hr><p>
Functions are pretty much the same, except it has a return value. It just
like mathematic functions. Syntax :</p><pre>
function funcname : returntype;
begin
:
{ commands }
:
:
end;
begin
end.
</pre><p>Functions are also placed before the main <tt>begin...end</tt>.
Example :</p><hr><pre>
uses crt;
function yesno : boolean;
var c : char;
begin
write('Are you sure (Y/N) ? ');
repeat
c:=upcase(readkey); { Make it upcase letters }
until (c='Y') or (c='N');
writeln(c);
if c='Y' then yesno:=true else yesno:=false;
end;
var
x : boolean; { Don't be tricked, this is a LOCAL variable of main block }
begin
clrscr;
writeln('Discard all changes made');
x:=yesno;
if x=true then
writeln('Changes discarded !');
else
writeln('Changes saved !');
writeln('Quitting');
x:=yesno;
if x then
begin
writeln('Process terminated !');
halt;
end;
writeln('Quit cancelled !');
writeln('Continuing process ...');
end.
</pre><hr><p>
Can you see a big difference between procedures and functions ? Use function
if we want to get a return value. Suppose the yesno function. It returns
true if user presses y and returns false if user presses n.
Yes, yes, it is pretty complex. But, try to understand. Or... try to understand
this simple excerpt :
(This is only the <tt>begin...end</tt> part. All above is the same as the previous
one.)</p><hr><pre>
begin
x:=yesno;
if x then
writeln('You answered "Yes"')
else
writeln('You answered "No"');
end.
</pre><hr><p>
OK ! Any questions ? Send them to <a href="../faq.html">me</a> !
Let's continue...</p>
<p>In a well-structured program, we use global variables as minimal as possible
and use the optimum amount of local variables. How can we ? Well, practice a lot !
Sometimes, we need some value to be known inside a procedure. In that case,
we need parameters. We can put parameters just beside the procedure's or
function's name, suppose :</p><hr><pre>
procedure myproc (a:integer; b:real);
begin
:
:
end;
function abc (a,b : integer; c:byte) : longint;
begin
:
:
end;
</pre><hr><p>Let's see it in 'real life' !</p><hr><pre>
uses crt;
procedure makewin(x1,y1,x2,y2 : byte);
var
i,j : byte;
begin
{ top }
gotoxy(x1,y1); write(#201);
for i:=x1+1 to x2-1 do write(#205);
write(#187);
{ middle }
for i:=y1+1 to y2-1 do
begin
gotoxy(x1,i); write(#186);
for j:=x1+1 to x2-1 do write(' ');
write(#186);
end;
{ bottom }
gotoxy(x1,y2); write(#200);
for i:=x1+1 to x2-1 do write(#205);
write(#188);
end;
begin
makewin(1,1,30,8);
makewin(10,10,60,18);
end.
</pre><hr><p>
Simple example above tell us about making a window in a coordinate (x1, y1)
to (x2, y2). With this engine, we could make many windows with just entering
coordinates and the SAME code. Parameters makes us easier to customize
the procedures and functions. Let's see this example :</p><hr><pre>
function factorial(n : byte):longint;
var
i : byte;
result : longint;
begin
result:=1;
for i:=1 to n do
result:=result*i;
factorial:=result;
end;
var
x : byte;
begin
writeln('Enter a value : '); readln(x);
writeln(x,'! is ',factorial(x));
end.
</pre><hr><p>
Those previous examples are widely used in programming life. Let's look at
this :</p><hr><pre>
procedure foo(a : byte);
begin
writeln(a); {15}
a:=10;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -