Functions of multiple parameters

Kapitoly: What is a function, Functions of multiple parameters, Folding functions, Graph a function

The simplest functions have only one parameter. They are good to work with and good to analyze. But in life, we sometimes need more complex functions that have more parameters.

What a multi-parameter function looks like

So far we have been working with functions that have one parameter. However, a function can easily have two parameters, or even more. Such a function is handled in the same way as a one-parameter function, except that, in the spreadsheet analogy, we have multiple columns for inputs, but there is one column for output. Such a table might look like this:

$$\begin{array}{ccc} \mbox{ Input and }&\mbox{ Input b }&Výsledek\\\hline 1&1&2\\ 1&2&3\\ 1&5&6\\ 3&5&8\\ 4&12&16\\ \end{array}$$

The labeling of such a function would look similar to that of a one-parameter function, except that we write two parameters in parentheses: f(a,b). The call would then look like f(1, 2). The evaluation procedure would look like finding a row where Vstup a = 1 and Vstup b = 2. On such a row, the Výsledek column has a value of 3, so f(1, 2) = 3.

Note that there are some values multiple times in the input and column. We've already established that a function must always return the same result for the same input. Does this apply here if we have the same values in the same input column? It does, because the second column distinguishes them for us. So it's okay to have the number 1 in the first column multiple times, because each time it has a different value in the second column. We call functions with two arguments, so it must be true that for every same pair of arguments, the function must return the same result. A bad table would look like this:

$$\begin{array}{ccc} \mbox{ Input and }&\mbox{ Input b }&Výsledek\\\hline 1&1&2\\ 1&2&3\\ 1&5&6\\ 3&4&8\\ 3&4&13\\ \end{array}$$

The last two rows have the same values in the inputs, so this table is not a valid function definition. This function would not know what to return if we called it with the arguments f(3, 4).

Examples of multi-parameter functions

In mathematics, a function of two parameters is a power function, for example. A power function takes a base as one argument and an exponent as the other. To be clear, the notation x2 is really a call to a two-parameter function. If we wanted to manually define the multiplication as a function, we could do it like this:

$$ \mbox{ power }(a, b) = a^b $$

The parameter a is the base and the parameter b is the exponent. Thus, we could also write x2 as mocnina(x, 2). In the body of the function, the variable x would be substituted for the parameter a and the exponent 2 would be substituted for the parameter b. And we have back the expression x2.

Another example might be the classic addition or multiplication operations. Addition or multiplication is nothing but a function with two parameters:

$$\begin{eqnarray} \mbox{ Retrieved from }(a, b) &=& a + b\\ \mbox{ nasobeni }(a, b) &=& a \cdot b \end{eqnarray}$$