Difference between revisions of "Procedure"

From Free Pascal wiki
Jump to navigationJump to search
m (substitute legacy syntaxhighlight syntax; unify source code style; remove unrelated/too general see-also-link)
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Procedure}}
 
{{Procedure}}
  
== Overview ==
+
A '''procedure''' is a [[Routine|routine]] that does not return a value.
A '''procedure''' is a declaration of a [[Routine|routine]] which may be invoked from within the [[Unit|unit]] that declares it, from outside the unit if the procedure is public (declared in [[Interface|interface]] section), or from within a [[Program|program]], and the routine does not return a value as part of its definition. A procedure that does return a value as part of its definition is called ''[[Function|function]]''.  A procedure that is part of an object is called a [[Method|method]].  A function that is part of an object is also a [[Method|method]] if it cannot be assigned a value from outside of the function, and is a [[Property|property]] if it can be assigned a value from outside of the function.
+
<syntaxhighlight lang="pascal" inline>procedure</syntaxhighlight> is a [[Reserved word|reserved word]].
  
You can also use procedure as type definition for your variables,which by it you can make your variables being treated as procedures.You can also use function for functions and procedure/function(..) of object for methods.
+
== Prematurely leaving a procedure ==
 +
In a procedure the routine {{Doc|package=RTL|unit=system|identifier=exit|text=<syntaxhighlight lang="pascal" inline>exit</syntaxhighlight>}} can be called in order to (prematurely) leave the procedure.
 +
<syntaxhighlight lang="pascal" inline>exit</syntaxhighlight> may not be supplied with any parameters, since procedures do not return any value, but [[Function|functions]] do.
 +
Supplying a parameter to <syntaxhighlight lang="pascal" inline>exit</syntaxhighlight> inside a procedure definition will yield the compile-time error “Error: Procedures cannot return a value”.
  
 +
== Invocation ==
 +
Procedure calls are statements.
 +
They may not appear in expressions, since they do not produce a value of any kind.
 +
The following example highlights all lines with procedure calls.
  
[[Variable parameter]]s example:
+
<syntaxhighlight lang="pascal" line highlight="8,9,14,15">
 +
program procedureDemo(input, output, stderr);
  
<syntaxhighlight>
+
var
procedure swap(var c1,c2:char);
+
x: longint;
var c:char;
 
begin
 
  c:=c1;
 
  c1:=c2;
 
  c2:=c;
 
end;
 
  
var s:string;
+
procedure foo;
 
+
begin
begin
+
exit;
  s:='pit';  
+
inc(x);
  swap(s[1],s[3]);
+
end;
  writeln (s); // result is 'tip'
 
end.
 
  
 +
begin
 +
x := 42;
 +
foo;
 +
writeLn(x);
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Procedure  parameters ==
+
Note, that <syntaxhighlight lang="pascal" inline>foo</syntaxhighlight> contains unreachable code (<syntaxhighlight lang="pascal" inline>inc(x)</syntaxhighlight> is never executed because of the [unconditional] <syntaxhighlight lang="pascal" inline>exit</syntaxhighlight>).
 
 
* call by value
 
* [[Variable parameter]] (call by reference)
 
* out parameter
 
* Const parameter
 
* [[Default parameter]]
 
  
 
== See also ==
 
== See also ==
 
+
* [[Procedures|Tutorial: procedures]]
* [http://www.freepascal.org/docs-html/ref/refsu49.html Procedure statements]
+
* [https://www.freepascal.org/docs-html/ref/refsu53.html § “Procedure statements” in the Reference Guide]
* [http://www.freepascal.org/docs-html/ref/refse17.html Procedural types]
+
* [https://www.freepascal.org/docs-html/ref/refse17.html § “Procedural types” in the Reference Guide]
 
 
[[Category:Pascal]]
 
[[Category:Control Structures]]
 

Revision as of 10:00, 24 May 2020

Deutsch (de) English (en) suomi (fi) français (fr) italiano (it) русский (ru)

A procedure is a routine that does not return a value. procedure is a reserved word.

Prematurely leaving a procedure

In a procedure the routine exit can be called in order to (prematurely) leave the procedure. exit may not be supplied with any parameters, since procedures do not return any value, but functions do. Supplying a parameter to exit inside a procedure definition will yield the compile-time error “Error: Procedures cannot return a value”.

Invocation

Procedure calls are statements. They may not appear in expressions, since they do not produce a value of any kind. The following example highlights all lines with procedure calls.

 1program procedureDemo(input, output, stderr);
 2
 3var
 4	x: longint;
 5
 6procedure foo;
 7begin
 8	exit;
 9	inc(x);
10end;
11
12begin
13	x := 42;
14	foo;
15	writeLn(x);
16end.

Note, that foo contains unreachable code (inc(x) is never executed because of the [unconditional] exit).

See also