Содержание:
Theory: Procedures and Functions
Lection in pdf format
Labs and tasks
Procedures
Parameterless procedure and with parameters
To do: Print 60 asterisks (star *), each on a new line. Use parameterless procedure.
✍ Решение:
procedure pr(a:char); {a is a formal parameter} begin loop 60 do begin println(a); end; end; begin writeln('enter character:'); var s:= readchar; pr(s); {s is an actual parameter} end. |
{0.3 points}
Task 1, procedures:
To do: Create a procedure that sums any two entered integers (two parameters).
Note: the signature or header of the procedure must be as follows:
procedure PrintSum(a, b: integer); |
The expected output:
enter two numbers: >>> 2 >>> 7 result: 2 + 7 = 9
[Program name: task-01-proc.pas
]
{0.3 points}
Task 2, procedures:
To do: Create a procedure that displays in a column all the integers between N
going down to 0
(starting with N
).
Note: the signature or header of the procedure must be as follows:
procedure PrintIntegers(n: integer); |
The expected output:
please, enter value of N: >>> 8 8 7 6 5 4 3 2 1 0
[Program name: task-02-proc.pas
]
{0.4 points}
Task 3, procedures:
To do: Create a procedure PrintSeq(A,B,C)
(A<B) that prints the sequence of numbers from A
to B
with a step C
(A
, B
and C
are input parameters).
Note: the signature or header of the procedure must be as follows:
procedure PrintSeq(A,B,C: integer); |
The expected output:
please, enter numbers A, B, C: >>> 2 >>> 9 >>> 2 2 4 6 8
[Program name: task-03-proc.pas
]
{0.5 points}
Task 4, procedures:
To do: Create a procedure that prints all the divisors of the number passed to it (the numer is entered in the main program and passed in to the procedure as a parameter).
Note: the signature or header of the procedure must be as follows:
procedure PrintDivisors(numb: integer); |
The expected output:
please, enter number: >>>18 1 2 3 6 9 18
[Program name: task-04-proc.pas
]
Input and Output parameters
To do: Let’s create a subroutine for calculating the arithmetic mean (average) of two entered integer values.
✍ Algorithm:
procedure CalcAMean(a, b: integer; var Mean: real); // Mean is an output parameter begin Mean := (a + b) / 2; end; begin var (x, y) := (3, 5); var Mean: real; CalcAMean(x, y, Mean); Print(Mean); CalcAMean(2 * 2, 8, Mean); Print(Mean); end. |
To do: Create a procedure to swap the values of two variables.
The expected output:
a = 10 b = 12 Result: a=12, b=10
✍ Algorithm:
-
Solution 1:
procedure Swap(var a, b: integer); begin var t := a; a := b; b := t; end; begin var (x, y) := (3, 5); Swap(x, y); println(x,y); end. |
Solution 2:
procedure Swap(var a, b: integer); begin (a, b) := (b, a); end; begin var (x, y) := (3, 5); Swap(x, y); println(x, y); end. |
{0.3 points}
Task 5, procedures:
To do: Create a procedure PlusN(N, result)
that calculates N
+ N
and passes the result in the result
parameter (N
is input parameter, result
is output parameter).
Note: the signature or header of the procedure must be as follows:
procedure PlusN(N : integer; var result: integer); |
The expected output:
please enter an integer n >>>5 result: 10
[Program name: task-05-proc.pas
]
{0.4 points}
Task 6, procedures:
To do: Create a procedure Power2N(N, powerN)
that calculates the power of a number (N
^powerN
) and passes the result in the result
parameter (N
and powerN
are input parameters, result
is an output parameter).
Note: the signature or header of the procedure must be as follows:
procedure PowerN(n, powerN: real; var result: real); |
The expected output:
please enter number and exponent >>>3 >>>4 3^4 = 81
[Program name: task-06-proc.pas
]
{0.4 points}
Task 7, procedures:
To do: Create a procedure FindMin(a,b, min)
that finds the minimum of two entered numbers a
and b
and passes the result in min
parameter (a
and b
are input parameters, min
is an output parameter).
The expected output:
enter two integers for a and b: >>>2 >>>6 min is 2
[Program name: task-07-proc.pas
]
{0.4 points}
Task 8, procedures:
To do: Create a procedure AddLeftDigit(D, K, res)
that appends entered digit K
(0<=K<=9) on the left to entered digit D
(0<=D<=9) (D
and K
are input parameters, res
is output parameter). Pass the result to the main program in the res parameter.
The expected output:
enter values for D (0<=D<=9) and K(0<=K<=9): >>>2 >>>4 result: 42
[Program name: task-08-proc.pas
]
{0.5 points}
Task 9, procedures:
To do: Create a procedure FindSum(N, sum)
that calculates the addition (sum) of N
generated numbers. Result is passed to the main program in sum
parameter (N
is an input parameter, sum
is an output parameter).
The expected output:
Please, input how many numbers to add: >>>5 generated numbers: 7 3 2 8 12 sum is 32
[Program name: task-09-proc.pas
]
{1.5 points}
* ExtraTask 10, procedures:
To do: Create a procedure AddLeftDigits(D, K1,K2, res1, res2)
that first appends entered digit K1
(0<=K1<=9) to entered positive integer D
and passes the result in res1
parameter, and, afterwards, appends digit K2
to it and passes the result in res2
.
The expected output:
Please, input D, K1 and K2 (0<=K<=9): 20 4 5 >>> 204 2045 --- 342 7 1 >>> 3427 34271
[Program name: task-10-proc.pas
]
Functions
To do: Find the maximum among two numbers using the function.
The expected output:
✍ Algorithm:
function max(a, b: integer): integer; begin if a > b then Result := a else Result := b; end; begin var (x, y) := readinteger2; println('maximum = ', max(x, y)) end. |
{0.3 points}
Task 1, functions:
To do: Create a function to find a minimum among three numbers (three parameters of the function).
The expected output:
please, input three numbers >>>2 >>>5 >>>9 minimum = 2
[Program name: task-01-func.pas
]
{0.3 points}
Task 2, functions:
To do: Create a function to find an average value (the arithmetic mean) of three entered numbers (three parameters).
The expected output:
please, input three numbers >>>2 >>>5 >>>2 average = 3
[Program name: task-02-func.pas
]
{0.3 points}
Task 3, functions:
To do: Create an IsDigit(D)
function, which returns true
if entered integer D
is a digit (that is D
is in the range [0,9]). In the main program output the results of this function for 3 entered numbers.
The expected output:
please, input number #1 42 false --- please, input number #2 4 true --- please, input number #3 -5 true
[Program name: task-03-func.pas
]
To do: Create a function to calculate a factorial of entered number.
The expected output:
Enter number: >>>5 5! = 120
✍ Algorithm:
function Fact(n: integer): integer; begin Result := 1; for var i := 1 to n do Result *= i end; begin var x:=readinteger('enter number:'); println($'{x}! = {Fact(5)}' ) end. |
{0.4 points}
Task 4, functions:
To do: Create a function that calculates the sum of all numbers from 1
to N
. N
is a function parameter.
The expected output:
Enter the number N: 100 the sum = 5050
[Program name: task-04-func.pas
]
To do: Create a function to calculate a hypotenuse of triangle (the sides of triangles are entered). Use this function to calculate the hypotenuse of 5 triangles.
✍ Algorithm:
function Hypot(a, b: real) := Sqrt(a * a + b * b); begin loop 5 do begin var (a, b) := ReadlnReal2('Enter sides a,b:'); Println(Hypot(a, b)) end; end. |
{0.4 points}
Task 5, functions:
To do: Create a CircleS(R)
function of real type that returns the area of a circle with radius R
(R
is entered real). Use this function to find the area of three circles with these radii. The area of a circle of radius R
is calculated by the formula S=π*R2
. As the value of PI (π)
, use 3.14
. Use short function definition.
The expected output:
Enter radius >>>5 S = --- Enter radius >>>7.8 S =
[Program name: task-05-func.pas
]
{0.4 points}
Task 6, functions:
To do: Create a function that calculates a power of a number. A base number and exponent are the parameters of the function. Use short function definition.
Note: To use the power()
function, remember that parameters of this function are reals.
The expected output:
Enter number and exponent: >>>5 >>>2 5 ^ 2 = 25
[Program name: task-06-func.pas
]
{0.5 points}
Task 7, functions:
To do: Create a the Triangle(a, h)
function that calculates a perimeter of an isosceles triangle by its base a
and height h
drawn to the base (a
and h
are entered integers). By calling this function output the perimeters of the three triangles (perimeter = sum of the lengths of all the sides). To find the side b
of a triangle, use the Pythagorean theorem: b2=(a/2)2+h2
.
The expected output:
Enter a and h >>>5 >>>2 perimeter = 11.40
[Program name: task-07-func.pas
]
{0.3 points}
Task 8, functions:
To do: Complete the previous task (# 7) by adding an overloading function to calculate a perimeter of an isosceles triangle for real values of a
and h
. Call this function twice — for integer arguments and real arguments.
The expected output:
Enter a and h >>>5 >>>2 perimeter = 11.40 --- Enter a and h >>>2.5 >>>6.8 perimeter = 16.33 --- Enter a and h >>>15.2 >>>18.3 perimeter = 54.83
[Program name: task-08-func.pas
]