Under
parameter,
rp was unsure how
Pascal referred to its
subroutines,
functions,
procedures, or whatever they were called.
Pascal has two types of what most 'normal' programming languages* call the same thing...
function UpCaseStr(S: string): string;
var
I: Integer;
begin
for I := 1 to Length(S) do
if (S[I] >= 'a') and (S[I] <= 'z') then
Dec(S[I], 32);
UpCaseStr := S;
end;
begin
writeln UpCaseStr("AbCdEfGhIj");
end;
This code fragment (partially borrowed from the
Delphi 2.0 online help for function) displays what a function does. It returns a single value, and allows the function to be called in-line, allowing ease of
coding. The S is a
parameter passed to the function.
procedure NumString(N: Integer; var S: string);
var
V: Integer;
begin
V := Abs(N);
S := '';
repeat
S := Chr(N mod 10 + Ord('0')) + S;
N := N div 10;
until N = 0;
if N < 0 then
S := '-' + S;
end;
var MyString:string;
begin
NumString(8,MyString);
writeln "That number, "+MyString+", is now a string.";
end;
This code fragment shows how a procedure differs. Firstly, it cannot be called inline. Secondly, it shows the difference between the two parameters. The first, the number, is a
value parameter: only the value is sent to the procedure, as the parameter was in the function. The second, the string, is a
reference parameter: the location of the string in memory (and thus the actual string itself) is passed to the procedure.
Personally, I feel this is a poor example of the ability of a procedure, since it has one reference parameter/output, and could have been coded as
function NumString(N: Integer;):string;
var
V: Integer;
S: string;
begin
V := Abs(N);
S := '';
repeat
S := Chr(N mod 10 + Ord('0')) + S;
N := N div 10;
until N = 0;
if N < 0 then
S := '-' + S;
NumString:=S;
end;
begin
print "That number, "+NumString(8)+", is now a string.";
end;
. Procedures are better in many cases, since you have more control over it: you can have multiple outputs, an input that changes, or no output at all. But functions are much neater.
*: QBasic uses subroutines and functions. That isn't a normal programming language at all.
Thanks to gorgonzola who pointed out that its write or writeln in pascal, not print... I'm in perl mode again!