Functions and Result in Delphi
Two functions that answer with a value by assigning to Result.
program FunctionsResult;
// A function answers with a value: assign it to Result.
function TideRange(Crest, Trough: Double): Double;
begin
Result := Crest - Trough;
end;
// Result may be set, then refined before the function returns.
function Describe(Meters: Double): string;
begin
Result := 'moderate';
if Meters < 2.0 then
Result := 'gentle';
end;
begin
WriteLn('range ', TideRange(5.4, 1.1):0:1, ' m');
WriteLn(Describe(TideRange(5.4, 1.1)), ' tide'); // a call inside a call
end.
How it works
TideRange(Crest, Trough: Double)takes two parameters and returns the difference.DescribesetsResult := 'moderate', then overwrites it whenMeters < 2.0.- The last line calls
TideRangeinside the argument list ofDescribe.
Keywords and builtins used here
DescribeDoubleResultTideRangeWriteLnbeginendfunctionifprogramstringthen
The run, in numbers
- Lines
- 19
- Characters to type
- 487
- Tokens
- 101
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 81 seconds.
Step 2 of 3 in Procedures & functions, step 11 of 27 in Language basics.