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

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

## Theory: Procedures and Functions

Lection in pdf format

### Input validation: Assert statement

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.

### 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.

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

Note: to print correct results you must unpack them.

The resulting example:

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

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.

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

results with function:
3 7
results with procedure:
3 7

### Functions as algorithm wrappers

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

The resulting example:

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

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

The resulting example:

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

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

The resulting example:

>>>173
sum = 11

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

>>>35
false

>>>3
true

>>>9
true

>>>10
false

### 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.

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

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

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

results with function:
3 7

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

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

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

>>>173
sum = 11

$${}$$