Lesson #13. Procedures
- Procedure is a piece of a code that performs a specific task.
- Procedure consists of header (name and parameters) and body.
- Procedure must be defined only once and can be run many times in the main program.
- Procedure must be defined before the
beginkeyword of the main program. - To run a code of the procedure one has just to call the procedure in the main program.
Task. Let's write a procedure to calculate an average of two
integer values (a & b) and to print the result:
Procedure definition before the program:

To run this procedure we have to call it and to pass two values in the main program:
****************************************************************************************
Example:
Task: Output a sequence of numbers from 1 to N (1 2 3 4 .... N).
Use the procedure.
Solution:
procedure PrintN(n: integer);
begin
for var i:=1 to n do
Print(i)
end;
begin
println('please, input N');
var n:=readinteger;
PrintN(N)
end.
Result:
please, input N 5 1 2 3 4 5******************************************************************************
✎
1. {0.3 points}[L13-task-01.pas] Define the procedure: IncN(N), which calculates N + 1 and prints the result (N is input parameter).
2. {0.3 points}[L13-task-02.pas] Define the procedure: PlusN(N), which calculates N + N and prints the result (N is input parameter).
3. {0.4 points}[L13-task-03.pas] Define the procedure: APlusB(A,B), which calculates A + B and prints the result (A and B are input parameters).
4.{0.5 points}[L13-task-04.pas] Define the procedure: PrintSeq(A,B) (A<B),
which prints the sequence of numbers from A to B (A and B are input parameters).
please, enter two numbers: 2 8 2 3 4 5 6 7 8
5. {0.6 points}[L13-task-05.pas] Define the procedure: PrintSeq(A,B,C)
(A<B), which prints the sequence of numbers from A to B with a step C
(A, B and C are input parameters).
please, enter two numbers and a step: 2 17 3 2 5 8 11 14 17
6. {0.6 points}[L13-task-06.pas] Define the procedure: PrintNumber(N,B) , which prints some entered number ( B Ntimes ( B and N are input parameters).
please, enter two numbers: 5 3 result: 3 3 3 3 3
*****************************************************************************************
Input and Otput parameters
Task. Let's write a procedure to calculate an average of two integer values (a & b); the procedure has to pass the result to the main program:


To run the procedure you have to call it in the main program:
**************************************************************************************
Formal & Actual parameters
Formal parameters - are the variables declared in a procedure definition.
Actual parameters - are the values passed to the procedure while calling it in the main program.
One more exaple:
***************************************************************************************************
✎
7. {0.5 points}[L13-task-07.pas] Define the procedure: PlusN(N, result), which calculates N + N and passes the result in the result parameter (N is input parameter, result is output parameter).
8. {0.5 points}[L13-task-08.pas] Define the procedure: APlusB(A, B, result), which calculates A + B and passes the result to the program in the parameter result (A and B are input parameters, result is output parameter).
9. {0.5 points}[L13-task-09.pas] Define the procedure: Power2N(N, power), which squares the number N (N*N) and passes the result in the parameter power (N is input parameter, power is an output parameter).
10. {0.5 points}[L13-task-10.pas] Define the procedure: FindMin(a,b, min), which finds the minimum between two numbers a and b and passes the result in the parameter min (a b are input parameters, min is an output parameter).
Please, input a and b: 2 15 >>> minimum is 2
11. {1.5 points}[L13-task-11.pas] Define the procedure: Mean(X, Y, AMean, GMean), which calculates:
the average of two real numbers X & Y:
and the proportional average (geometric mean) of two real numbers X & Y:
![]()
![]()
X и Y are input parameters; while AMean and GMean are output parameters of real type.
In the main program: For given A, B, C, D find the average and the proportional average (geometric mean) of three pairs (A, B), (A, C), (A, D). Use a procedure.
Please, input A, B, C, D: 2 4 3 6 >>> A, B: AMean = 3 GMean = 2.828 >>> A, C: AMean = 2.5 GMean = 2.449 >>> A, D: AMean = 4 GMean = 3.464
12. {1.5 points}[L13-task-12.pas] Define the procedure: AddLeftDigit(D, K, res), which appends digit K (0<=K<=9) to the positive integer D (0<=D<=9) (D , K are input parameters, res is output parameter). Pass the result to the main program in the res parameter:
Please, input D (0<=D<=9) and K (0<=K<=9): 2 4 >>> 24
13. {0.7 points}[L13-task-13.pas] Define the procedure: FindSum(N, sum), which finds the addition (sum) of N numbers. Result is passed to the main program in the parameter sum (N is an input parameter, sum is an output parameter).
Please, input how many numbers will be (N): 5 input numbers: 7 3 2 8 12 >>> sum is 32
14. {0.7 points}[L13-task-14.pas] Define the procedure: FindAverage(N, aver), which finds the average among N numbers. Result is passed to the main program in the parameter aver.
Please, input how many numbers will be(N): 5 input numbers: 7 3 2 8 15 >>> average is 7**********************************************************************************
Extra tasks:
1. {1.5 points}[L13-task-01-extra.pas] Define the procedure: AddLeftDigit(D, K, res), which appends digit K (0<=K<=9) to the positive integer D (D, K are input parameters, res is output parameter). Pass the result to the main program in the res parameter:
Please, input D and K (0<=K<=9): 20 4 >>> 204 342 7 >>> 3427
2. {1.5 points}[L13-task-02-extra.pas] Define the procedure: AddLeftDigits(D, K1,K2, res1, res2), which first appends digit K1 (0<=K1<=9) to the positive integer D and passes the result in res1, and afterwards appends digit K2 to it and passes the result in res2:
Please, input D, K1 and K2 (0<=K<=9): 20 4 5 >>> 204 2045 342 7 1 >>> 3427 34271
3. {1.5 points}[L13-task-03-extra.pas] Define the procedure: FibN(N, result), which passes to the main program the N-th number of Fibonacci sequence. Fibonacci sequence: 1,1,2,3,5,8,13,21,34,55,… (F1=1, F2=1, Fk = Fk−2 + Fk−1, k = 3,4,…).
6 >>> 8 9 >>> 34