Lesson #11. Procedures and functions: tuples, algorithm wrappers, Local and global variables

Theory: Procedures and Functions

Lection in pdf format

Input validation: Assert statement

Task Example:

To do: Create the Mean (X, Y, AMean, GMean) procedure that calculates the arithmetic mean AMean = (X + Y) / 2 and the geometric mean GMean = (X Y) 1/2 of two positive numbers X and Y (X and Y are entered, AMean and GMean are real output parameters).

✍ Решение:

  • The programmer must «secure» the program against incorrect input data.
  • The positivity of the parameters X and Y is necessary for calculating the geometric mean, which occurs inside the Mean procedure. So, the check must be inside the Mean procedure:
  • /// calculates aMean and gMean
    procedure Mean(...);
    begin
      Assert(x > 0);
      Assert(y > 0);
      // calculating AMean, GMean
      // ...
    end;
    
  • Mean procedure has two input and two output parameters. This means that each test case must check that for the given X and Y values, both output parameters have the expected values. I.e:
  • you need to set the values of the input parameters (X, Y);
  • call the Mean procedure by passing these input values to it;
  • check that the values of the output parameters (AMean, GMean) are the same as expected.
  • /// returnes true, if the reals x and y are equaled with precision eps
    function AreEquals(x, y: real; eps: real): boolean;
    begin
      Result := Abs(x - y) < eps;
    end;
     
    /// Tests the Mean procedure
    procedure TestMean(eps: real);
    begin
      var x, y: real;
      var am, gm: real;
      // test 1: equal values
      x := 2;
      y := x;
      Mean(x, y, am, gm);
      Assert(AreEquals(am, x, eps) 
        and AreEquals(gm, x, eps));
      // test 2: different values
      Mean(3, 27, am, gm);
      Assert(AreEquals(am, 15, Eps) 
        and AreEquals(gm, 9, Eps));
      // test 3: different values
      Mean(6.05, 5, am, gm);
      Assert(AreEquals(am, 5.525, Eps) 
        and AreEquals(gm, 5.5, Eps));
    end;
     
    const Eps = 0.00000001;
     
    begin
      // call of the test procedure
      TestMean(Eps);
     
      // main program
      // ...
    end.

Labs and tasks

Function and tuples

Lab, Functions that return tuples:

To do: Create a function to print sum and product of two numbers.

✍ Algorithm:

    function PrintSumMult(x, y: real): (real, real);
    begin
      Result := (x + y, x * y);
    end;
     
    begin
      var (a, b) := PrintSumMult(2, 4);
      Print(a, b); // 6  8
    end.
{0.4 points} Task 1:

To do: Find a minimum and maximum among 3 numbers. Create a function to do the task. The function return type is a tuple (min and max).

Note: to print correct results you must unpack them.

The expected output:

enter three numbers:
>>>3 >>>5 >>>1  
min = 1, max = 5
---
enter three numbers:
>>>7 >>>0 >>>6  
min = 0, max = 7

[Program name: 11task-01.pas]

Lab, Short definitions + tuples:

To do: Create a procedure and function to print sum and product of two numbers. Use short definition.

✍ Algorithm:

    Fucntion:

    function PrintSumMult(x, y: real) := (x + y, x * y);
    begin
      var (a, b) := PrintSumMult(2, 4);
      Print(a, b); // 6 8
    end.

    Procedure:

    procedure PrintSumMult(x, y: real; var add, mult: real);
    begin
      add := x + y;
      mult := x * y;
    end;
    begin
      var (x, y) := ReadReal2; 
      var a, b: real;
      PrintSumMult(x, y, a, b);
      Print(a, b); 
    end.
{0.5 points} Task 2:

To do: Create procedure and function inside the same program to print a left digit and right digit of two-digit number (the number is entered and passed in as a parameter).

Note 1: You must use short definitions.

Note 2: Procedure and function must be defined one by one before the main program.

The expected output:

please, enter two-digit number: 37
results with function: 
3 7 
results with procedure: 
3 7 

[Program name: 11task-02.pas]

Functions as algorithm wrappers

{0.3 points} Task 3:

To do: Create a function to output the day of the week by it’s sequence number.

The expected output:

please, enter a number 1 - 7:
>>>3  
Tuesday
---
please, enter a number 1 - 7:
>>>1  
Sunday

[Program name: 11task-03.pas]

{0.4 points} Task 4:

To do: Create a function to calculate the quantity of positive numbers among n generated numbers (random() function must be used).

The expected output:

How many numbers?
>>> 5 
2   4  -3   5  -4 
3 positive numbers

[Program name: 11task-04.pas]

{0.5 points} Task 5:

To do: Create a function to calculate an addition of digits of entered integer number (entered number is a parameter of the function).

The expected output:

please, enter an integer:
>>>173
sum = 11

[Program name: 11task-05.pas]

{0.4 points} Task 6:

To do: Create IsDigit(A) function, which returns true if entered integer A represents a digit (that is A is in the range [0,9]). In the main program call this function for 4 given numbers (the function call has to be inside a loop).

The expected output:

Please, enter the number:
>>>35 
false

Please, enter the number:
>>>3 
true

Please, enter the number:
>>>9 
true

Please, enter the number:
>>>10 
false

[Program name: 11task-06.pas]

Assert statement

Lab, assert:

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 N entered numbers.

✍ Algorithm:

    function IsDigit(d : integer):= (d >= 0) and (d <= 9);
     
    procedure TestIsDigit;
    begin
      for var i := 0 to 9 do
        Assert(IsDigit(i)=true,'incorrect function algorithm');
    end;
    begin
      TestIsDigit;
     
      var N := ReadInteger();
      Assert(n >= 0);
     
      for var i:=1 to n do
      begin
        var a := ReadInteger();
        Print(IsDigit(a));
      end;
    end.
{0.3 points} Task 7:

To do: Open the code of solution of Task 1 (about minimum and maximum). Add to your code the procedure to test the correctness of the function (look at the note below). Call this procedure inside the main program. Check the output.

Note: This code must be added to your solution. Fulfil TODO places:

 
procedure TestFindMinMax; // procedure to test the correctness of the function
begin
  Assert(findMinMax(4, 1, 5) = (1,5), 'algorithm of findMinMax function is not correct');
  Assert(findMinMax(-5, 500, 5) = (-5,500), 'algorithm of findMinMax function is not correct');
  // TODO: add 3 or 4 your own tests
end;
 
begin // main program
  TestFindMinMax;
  //...
end.

The expected output:

enter three numbers:
>>>3 >>>5 >>>1  
min = 1, max = 5
---
enter three numbers:
>>>7 >>>0 >>>6  
min = 0, max = 7

[Program name: 11task-07.pas]

{0.6 points} Task 8:

To do: Open the code of solution of Task 2. Create the procedure to test the correctness of your function. Call this procedure inside the main program. Check the output.

Note: Look at the example of the Lab (Assert) to check: 1) if entered number is two-digit number; 2) and if the test procedure returns the correct results (create 2 or 3 tests must be done):

function IsTwoDigit(...):= ...;
procedure Test...; // procedure to test the correctness of the function
begin
  Assert(..., 'error: the function is incorrect'); // test #1
  Assert(..., 'error: the function is incorrect'); // test #2
 
end;
 
begin // main program
  Test...;
  var numb:=readinteger('please, enter two-digit number:');
  Assert(IsTwoDigit(numb), 'error: number must be of two digits');
  //...
end.

The expected output:

please, enter two-digit number: 37
results with function: 
3 7 

[Program name: 11task-08.pas]

{0.5 points} Task 9:

To do: Open the code of solution of Task 3. Create the procedure to test the correctness of your function (3 or 4 tests must be done). Call this procedure inside the main program. Also, you must check the correctness of the input (the quantity of generated numbers must be greater than zero).

The expected output:

please, enter a number 1 - 7:
>>>3  
Tuesday
---
please, enter a number 1 - 7:
>>>1  
Sunday
---
please, enter a number 1 - 7:
>>>0
! exception: entered number must be in the interval 1 - 7

[Program name: 11task-09.pas]

{0.5 points} Task 10:

To do: Open the code of solution of Task 5. Create the procedure to test the correctness of your function (3 or 4 tests must be done). Call this procedure inside the main program.

The expected output:

please, enter an integer:
>>>173
sum = 11

[Program name: 11task-10.pas]