Lesson #10. Procedures and functions

Дата изменения: 31 октября 2020

Theory: Procedures and Functions

Lection in pdf format

Labs and tasks

Procedures

Parameterless procedure and with parameters

Lab 1, procedures:

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 resulting example:

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 resulting example:

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 resulting example:

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 resulting example:

please, enter number: 
>>>18
1 2 3 6 9 18 

[Program name: task-04-proc.pas]

Input and Output parameters

Lab 2, procedures:

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.
Lab 3, procedures:

To do: Create a procedure to swap the values of two variables.

The resulting example:

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 resulting example:

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 resulting example:

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 resulting example:

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 resulting example:

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 resulting example:

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 resulting example:

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

Lab 1, functions:
To do: Find the maximum among two numbers using the function.

The resulting example:

✍ 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 resulting example:

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 resulting example:

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 resulting example:

please, input number #1
42
false
---
please, input number #2
4
true
---
please, input number #3
-5
true

[Program name: task-03-func.pas]

Lab 2, functions:
To do: Create a function to calculate a factorial of entered number.

The resulting example:

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 resulting example:

Enter the number N:
100
the sum = 5050

[Program name: task-04-func.pas]

Lab 3, short function definition:
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 resulting example:

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 resulting example:

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 reals). 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 resulting example:

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-07-func.pas]

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

*
*


Вставить формулу как
Блок
Строка
Дополнительные настройки
Цвет формулы
Цвет текста
#333333
Используйте LaTeX для набора формулы
Предпросмотр
\({}\)
Формула не набрана
Вставить