const and var parameters in Delphi
Tune reads a const station name and writes back through a var parameter.
program ParamsConstVar;
// const promises the callee will not change the argument.
// var passes the caller's variable itself, so writes stick.
procedure Tune(const Station: string; var Dial: Integer);
begin
WriteLn('tuning to ', Station);
Dial := Dial + 3;
end;
var
Dial: Integer;
begin
Dial := 88;
WriteLn('before: ', Dial);
Tune('Radio Orkney', Dial);
WriteLn('after: ', Dial);
end.
How it works
procedure Tune(const Station: string; var Dial: Integer);mixes both kinds.Dial := Dial + 3;changes the caller's own variable rather than a copy.- Printing
Dialbefore and after the call shows 88 becoming 91.
Keywords and builtins used here
IntegerTuneWriteLnbeginconstendprocedureprogramstringvar
The run, in numbers
- Lines
- 18
- Characters to type
- 388
- Tokens
- 78
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 62 seconds.
Step 3 of 3 in Procedures & functions, step 12 of 27 in Language basics.