2.2 Some Turing Functions

In mathematics, a function is a process that takes some input and produces a single value. For example, y = x2 is a function. For each different value of x substituted into the equation there is only one value of y that is produced. Turing has several useful predefined functions that do the same sort of thing. In this section we will look at some of these.

sqrt

The sqrt function is the SQuare RooT function. You pass the function a number and sqrt returns the square root of that number as a real value. Here is a program that will give the square root of any value that is entered.

var num, root : real

put "Enter a number a number to square root"
get num
root := sqrt(num)
put "The square root of ", num , " is ", root 

If the user enters 5 then the output will be:

Enter a number a number to square root
5
The square root of 5 is 2.236068

We don't have to store the result of sqrt in a variable. In the above program we could have replaced the last two lines with: put "The square root of ", num , " is ", sqrt(num).
The main reason for using the variable was to emphasize that the result of sqrt is a real. You cannot store the square root of a number in an int. This attempt to calculate the 2 times the square root of x - 3 has an error because of how it tries to store the result.

var answer : int
var x : int := 5
answer := 2 * sqrt(x - 3) % error since this will produce a real value
put answer 

You can pass an int into sqrt but the produced result will always be a real.

abs

The abs function is the ABSolute value function. The absolute value function takes a number and returns its absolute value. The return type is the same as the parameter that is passed. If you do abs on an int you will get an int. If you abs a real you will get a real. The following program illustrates this:

var i : int
var x : real

i := abs (-16) % since -16 is an int so is the absolute value
x := abs (-10.652) % the absolute value will be real this time
put "The absolute value of -16 is ", i
put "The absolute value of -10.652 is ", x
put "The absolute value of a positive number is the same as the number"
put "For example, the absolute value of 105 is ", abs(105) 

Here is the output:

The absolute value of -16 is 16
The absolute value of -10.652 is 10.652
The absolute value of a positive number is the same as the number
For example, the absolute value of 105 is 105


Turing has quite a large number of other predefined functions. There are several trigonometric functions (such as sin and cos) and some logarithmic ones. We will not look at these in this course but it is useful to know that they exist.

Exercise 2.2

  1. Find the value of each expression.
    1. sqrt(9)
    2. sqrt(9.0)
    3. sqrt(0.0064)
    4. sqrt(8)
    5. abs(-5) - abs(-7)
    1. abs(-1e-1) + abs(-2e-2)
    2. sqrt(3 ** 2 + 4 ** 2)
    3. (sqrt(9) + sqrt(16)) ** 2
    4. sqrt(abs(-25))
    5. abs(sqrt(-25))
  2. Write a program that will find the roots of ax2 + bx + c = 0. Prompt the user for values of a, b and c. Use the quadratic formula to solve for the roots. Assume that the user will enter values that will produce real roots.