In Pascal, tasks are grouped into subprograms, which are called either Procedures or Functions. This modular approach improves readability and reduces code duplication.
1. Procedures (Actions)
A Procedure is a subprogram that executes a sequence of statements to perform an action, but it does not return a value to the main program or calling routine.
A. Declaration and Definition
Procedures are declared in the var or const section, but before the main begin block of the program or unit’s implementation.
// Declaration
procedure GreetUser;
// Definition (Implementation)
procedure GreetUser;
begin
Writeln('-------------------------');
Writeln('Welcome to the Pascal Demo.');
Writeln('-------------------------');
end;
// Usage in the main program block
begin
GreetUser; // Call the procedure
Writeln('Starting application...');
end.
2. Functions (Calculations)
A Function is a subprogram that executes a sequence of statements and is designed to compute and return a single value to the caller.
A. Declaration and Definition
Functions require a return type, specified after the parameter list. The return value is set using the special variable Result (or by assigning to the function’s name).
// Declaration
function CalculateArea(Width, Height: Real): Real;
// Definition (Implementation)
function CalculateArea(Width, Height: Real): Real;
begin
// Set the return value using the reserved 'Result' variable
Result := Width * Height;
end;
// Usage in the main program block
var
Area: Real;
begin
// Call the function and assign its return value to a variable
Area := CalculateArea(5.0, 12.0);
Writeln('The calculated area is: ', Area:1:2);
end.
3. Parameter Passing
Parameters are variables passed into a procedure or function, allowing the subprogram to operate on external data. Pascal supports two fundamental methods for passing parameters: Pass by Value and Pass by Reference.
A. Pass by Value (Default)
- Behavior: The default behavior. When a parameter is passed by value, a copy of the variable’s data is created for the procedure/function to use.
- Effect: Any changes made to the parameter inside the subprogram do not affect the original variable in the caller’s scope.
- Syntax: Simply list the variable name and type.
procedure IncrementValue(Value: Integer); // Passed by Value
begin
Writeln('Inside procedure (before): ', Value); // e.g., 10
Value := Value + 10;
Writeln('Inside procedure (after): ', Value); // e.g., 20
end;
var
A: Integer;
begin
A := 10;
IncrementValue(A);
Writeln('Original value after call: ', A); // Still 10!
end.
B. Pass by Reference (var Parameters)
- Behavior: Use the
varkeyword before the parameter name. The subprogram receives the memory address of the original variable, not a copy. - Effect: Any changes made to the parameter inside the subprogram will modify the original variable in the caller’s scope.
- Syntax: Precede the parameter name with
var.
procedure ResetValue(var Value: Integer); // Passed by Reference
begin
Writeln('Inside procedure: Resetting value.');
Value := 0; // Modifies the original variable
end;
var
A: Integer;
begin
A := 100;
ResetValue(A);
Writeln('Original value after call: ', A); // Is now 0!
end.
When to use
var: Usevarwhen a procedure needs to return more than one piece of data, or when passing large data structures (like arrays or objects) to avoid the time and memory overhead of copying the entire structure.
4. Forward Declarations
Pascal requires that any identifier (procedure or function) be declared before it is used. If Procedure A needs to call Procedure B, but Procedure B is defined later, you use the forward directive to declare B’s header early.
// 1. Forward declaration (tells the compiler the function exists)
procedure ProcB(Value: Integer); forward;
// 2. Definition of ProcA (can now safely call ProcB)
procedure ProcA(Value: Integer);
begin
Writeln('A is running: ', Value);
if Value > 0 then
ProcB(Value - 1);
end;
// 3. Final Definition of ProcB (without 'forward')
procedure ProcB(Value: Integer);
begin
Writeln('B is running: ', Value);
if Value > 0 then
ProcA(Value - 1);
end;
