typestar

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

  1. TideRange(Crest, Trough: Double) takes two parameters and returns the difference.
  2. Describe sets Result := 'moderate', then overwrites it when Meters < 2.0.
  3. The last line calls TideRange inside the argument list of Describe.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Procedures & functions, step 11 of 27 in Language basics.

← Previous Next →