How to declare a procedure in Pascal
Procedures in Pascal are declared using the procedure keyword, as shown in the code snippet below:
procedure _procName_ (_parameter(s)_: _parameterType_;, _parameter(s)_: _parameterType_;, ...);
_localDeclarations_
begin
//procedure body
end;
_procName_: The name of the procedure._argument(s)_: The arguments that are passed to the procedure._parameterType_: The data type of the arguments passed._localDeclarations_: Declarations such as variables, constants, functions, etc. that are referred to in theprocedure body.
Examples
Example 1
Consider a simple example of a procedure that calculates the sum of two numbers:
program Example1;vara, b, calSum : integer;procedure sum(a, b: integer; var calSum: integer);vartempSum: integer;//local DeclarationbegincalSum := a + b;end;begina := 5;b := 10;sum(a, b, calSum);writeln(a, ' + ', b, ' = ', calSum);end.
Explanation
- Lines 5-11: The
sum()procedure is declared that takes three parameters,a,b, andcalSum. Thesum()procedure calculates the mathematical sum ofaandbin line 10 and stores the sum in a variable parameter,calSum. AscalSumis a variable parameter, changing its value in the procedure will be propagated back to the main block. So, we can store the sum incalSumand access the sum in main. - Line 16: The
sum()procedure is called in line 16 and is passed witha,b, andcalSumas parameters.
Example 2
Consider an example of a procedure with no parameters:
program Example2;procedure noParam();beginwriteln('This is a procedure with no parameters');end;beginnoParam();end.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved