Difference between revisions of "Function"
(→return value: modeswitch) |
|||
Line 8: | Line 8: | ||
== return value == | == return value == | ||
− | In addition to a normal procedure, a function's formal signature contains a return type: | + | In addition to a normal procedure, a function's formal signature contains a return [[Data type|type]]: |
− | The formal parameter list has to be succeeded by a colon and return type. | + | The formal parameter list has to be succeeded by a [[Colon|colon]] and return type. |
For instance the following function returns a [[Boolean|<syntaxhighlight lang="pascal" enclose="none">boolean</syntaxhighlight>]]. | For instance the following function returns a [[Boolean|<syntaxhighlight lang="pascal" enclose="none">boolean</syntaxhighlight>]]. | ||
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
Line 29: | Line 29: | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | If <syntaxhighlight lang="pascal" enclose="none">{$modeswitch result+}</syntaxhighlight>, which is set by [[Mode ObjFPC|<syntaxhighlight lang="pascal" enclose="none">{$mode objFPC}</syntaxhighlight>]] and [[Mode Delphi|<syntaxhighlight lang="pascal" enclose="none">{$mode Delphi}</syntaxhighlight>]], inside the implementation block the special identifier <syntaxhighlight lang="pascal" enclose="none">result</syntaxhighlight> is available, too: | + | If <syntaxhighlight lang="pascal" enclose="none">{$modeswitch result+}</syntaxhighlight>, which is set by [[Mode ObjFPC|<syntaxhighlight lang="pascal" enclose="none">{$mode objFPC}</syntaxhighlight>]] and [[Mode Delphi|<syntaxhighlight lang="pascal" enclose="none">{$mode Delphi}</syntaxhighlight>]], inside the implementation block the special [[Identifier|identifier]] <syntaxhighlight lang="pascal" enclose="none">result</syntaxhighlight> is available, too: |
<syntaxhighlight lang="pascal" line start="13" highlight="4"> | <syntaxhighlight lang="pascal" line start="13" highlight="4"> | ||
// using special `result` identifier | // using special `result` identifier | ||
Line 47: | Line 47: | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | In assembly language other rules apply. | + | In [[Assembly language|assembly language]] other rules apply. |
If the return type is an integral value, the accumulator register is used, provided it fits in there: | If the return type is an integral value, the accumulator register is used, provided it fits in there: | ||
<syntaxhighlight lang="pascal" line start="25" highlight="9,16,28"> | <syntaxhighlight lang="pascal" line start="25" highlight="9,16,28"> | ||
Line 141: | Line 141: | ||
== see also == | == see also == | ||
− | * [https://www.freepascal.org/docs-html/ref/refch14.html | + | * [https://www.freepascal.org/docs-html/ref/refch14.html § “Using functions and procedures” in the Reference Guide] |
− | * [https://www.freepascal.org/docs-html/prog/progse10.html | + | * [https://www.freepascal.org/docs-html/prog/progse10.html § “Intel 80x86 Inline assembler” in the Programmer's Guide] |
− | |||
− |
Revision as of 08:50, 9 February 2019
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
русский (ru) │
A function is a routine that, in contrast to procedures, returns a value.
A call of a function is virtually substituted by its return value.
If the {$extendedSyntax}
compiler switch state is off, function calls can not appear as non-productive statements, but have to be or be part of an expression.
The word function
is a reserved word.
return value
In addition to a normal procedure, a function's formal signature contains a return type:
The formal parameter list has to be succeeded by a colon and return type.
For instance the following function returns a boolean
.
function myFunction(const firstParameter: real): boolean;
When implementing functions there are several ways to define the function's return value.
1program functionDemo(input, output, stderr);
2
3{$mode objfpc}
4
5// traditional syntax:
6// the result is stored in the variable
7// its name is the same as the function's
8function myLine(const x: real): real;
9begin
10 myLine := 0.5 * x + 2;
11end;
If {$modeswitch result+}
, which is set by {$mode objFPC}
and {$mode Delphi}
, inside the implementation block the special identifier result
is available, too:
13// using special `result` identifier
14function myParabola(const x: real): real;
15begin
16 result := sqr(x) - 1;
17end;
Additionally, in {$mode objFPC}
the routine exit
will set the return value, too, and leave the stack frame.
In the previous two examples further statements could have appeared, and they would have been executed, whilst after an exit
the routine is done.
This is the behavior a return
statement in C or other programming languages has.
19// using exit routine
20function even(const x: longint): boolean;
21begin
22 exit(not odd(x));
23end;
In assembly language other rules apply. If the return type is an integral value, the accumulator register is used, provided it fits in there:
25// in assembly language:
26// return type fits into a single register => use accumulator register
27function zero(const x: int64): boolean;
28{$ifdef CPUx86_64}
29assembler; register;
30{$asmMode intel}
31asm
32 // xor modifies flags => put it in front of test
33 xor rax, rax // rax := 0 [false]
34
35 // examining the assembler output
36 // we can verify x is stored in register rdi [i.e. not rax]
37 test x, x // x = 0 ?
38 jnz @zero_done // if x <> 0 then goto done
39
40 inc rax // rax := 1 [true]
41@zero_done:
42 // When you examine the assembler output
43 // you will notice the compiler automatically inserts code
44 // that moves the contents of rax to the right spot on the stack.
45end;
46{$else}
47begin
48 // NOTE: with optimization switches enabled
49 // the compiler produces with the following Pascal statement
50 // even shorter (and maybe faster) code
51 // than the assembler implementation above
52 result := x = 0;
53end;
54{$endif}
Otherwise, depending on which {$asmMode}
is active, the @result
(Intel) or __result
(AT&T) macro can be used.
56type
57 bodyAttributes = record
58 surfaceArea: real;
59 volume: real;
60 end;
61
62// in assembly language:
63// return type doesn't fit into accumulator => @result macro gives address
64function sphere(const radius: real): bodyAttributes;
65{$ifdef CPUx86_64}
66assembler;
67{$asmMode intel}
68const
69 three: longint = 3;
70 four: longint = 4;
71var
72 r: real;
73asm
74 pextrq r, radius, 0 // r := (@radius+0)^
75 lea rax, @result // rax := @result
76 fld r // radius
77
78 fld st(0) // radius radius
79 fild four // 4 radius radius
80 fldpi // pi 4 radius radius
81 fmul // 4*pi radius radius
82 fxch // radius 4*pi radius
83 fld st(0) // radius radius 4*pi radius
84 fmul // radius^2 4*pi radius
85 fmul // 4*pi*radius^2 radius
86 fst [rax].bodyAttributes.surfaceArea
87
88 fmul // 4*pi*radius^3
89 fild three // 3 4*pi*radius^3
90 fdivp // 4/3*pi*radius^3
91 fst [rax].bodyAttributes.volume
92end;
93{$else}
94begin
95 sphere.surfaceArea := 4 * pi() * sqr(radius);
96 sphere.volume := 4 / 3 * pi() * sqr(radius) * abs(radius);
97end;
98{$endif}
Originally Pascal expected exact one assignment to the result variable (whichever is used). FPC however does not prohibit multiple assignments. It will emit a warning, if none of the possible result identifiers were used or the exit routine is not written.
- Warning
- Function result does not seem to be set
- You can get this warning if the compiler thinks that a function return value is not set. This will not be displayed for assembler procedures, or procedures that contain assembler blocks.
100begin
101 writeLn(sphere(2.0).surfaceArea);
102end.