## Section2.2The Instruction return

When executing an algorithm, we follow the instructions in the algorithm until we encounter the instruction return. At return we leave the algorithm, and the values after the return statement are the output of the algorithm. The properties of these values must match what is given under Output. What the algorithm returns is the output of the algorithm. If an algorithm returns several values, we specify this by separating the output values by commas (see Algorithm 2.2.3 below). We give some examples of algorithms.

In these simple examples Output and the expression after return are essentially the same, which makes one of the seem redundant. In later examples it will become clearer that under Output we say what we are computing and that the sequence of instructions say how we compute it.

This algorithm has two integers as its input and returns their sum as its output:

We follow the instructions in Algorithm 2.2.1 for the input values $$a=57$$ and $$b=8\text{.}$$

Input: $$a=57$$ and $$b=8\text{.}$$ As both 57 and 8 are integers this a valid input for the algorithm.

1. return $$a+b$$ : The value of the variable $$a$$ is 57 and the value of the variable $$b$$ is 8. We compute $$a+b=57+8=65\text{.}$$ So the value after return is 65, it is the output of the algorithm.

Output: $$65$$

So when the input is $$a=57$$ and $$b=8$$ the output of Algorithm 2.2.1 is 65.

The following algorithm has four output values. The input of the algorithm is an integer $$c\text{;}$$ its output are the powers $$c\text{,}$$ $$c^2\text{,}$$ $$c^3\text{,}$$ and $$c^4\text{.}$$

We follow the instructions in Algorithm 2.2.3 for the input value $$c=-3\text{.}$$

Input: $$c=-3\text{.}$$ As $$-3$$ is an integer this a valid input for the algorithm.

1. Before returning $$c\text{,}$$ $$c\cdot c\text{,}$$ $$c\cdot c\cdot c\text{,}$$ $$c\cdot c\cdot c\cdot c$$ the values of the expressions have to be computed. As $$c$$ is equal to $$-3$$ we compute

\begin{align*} c\cdot c \amp = (-3)\cdot(-3)=9\\ c\cdot c\cdot c \amp =(-3)\cdot(-3)\cdot(-3)=-27\\ c\cdot c\cdot c\cdot c\amp =(-3)\cdot(-3)\cdot(-3)\cdot (-3)=81 \end{align*}

Thus the algorithms returns the values $$-3\text{,}$$ $$9\text{,}$$ $$-27\text{,}$$ and $$81\text{.}$$

Output: $$-3\text{,}$$ $$9\text{,}$$ $$-27\text{,}$$ $$81\text{.}$$

So when the input is $$c=-3$$ the output of Algorithm 2.2.3 is $$-3\text{,}$$ $$9\text{,}$$ $$-27\text{,}$$ and $$81\text{.}$$

This algorithm does not have any input and always returns 42 as its output:

An algorithm without a return instruction does not have any output.

In Checkpoint 2.2.6 complete the algorithm and determine the output value for the given input values.

Complete the algorithm and find the output for the given input values.

Algorithm

Input: two integers c and q

Output:

• select

• the sum of c and q

• the difference of c and q

• the product of c and q

• the negative of c

• the integer 16

.

(1) return c-q

Find the output of the algorithm for the input c := 3 and q := -6 :

Find the output of the algorithm for the input c := -1 and q := 2 :

Find the output of the algorithm for the input c := 5 and q := 0 :

$$\text{the difference of c and q}$$

$$9$$
$$-3$$
$$5$$