Can functions switch between data types?

133 Views Asked by At

I don't know what the right terminology is so I am using some programming jargon, sorry.

But for example can we say that a function accepts reals and returns naturals? Or accepts complex and returns reals? More generally accepting one type and returning another?

For example $f(x) = \sqrt{x}$ defined as having domain natural numbers and codomain natural numbers. Then I can feed it $\sqrt{16} = 4$ with no issue but then $\sqrt{5}$ is not a natural number.

Am I not allowed to mix and match input/return types? What are these properties even called?

4

There are 4 best solutions below

14
On BEST ANSWER

A function can take any "input type" and give any "output type". The "input type" is called the domain of the function, and the "output type" is called the codomain. The domain and codomain are allowed to be any sets at all, and do not have to be the same set.

To be more precise, given any two sets $A$ and $B$, a function $f:A\to B$ is a rule which, given any element $a\in A$, produces an output $f(a)$ which is an element of $B$. Here $A$ is the domain and $B$ is the codomain. (To be even more precise, such a "rule" is really a subset $S$ of $A\times B$, where for each $a\in A$ there is a unique $b\in B$ such that $(a,b)\in S$, in which case $b$ is what we call $f(a)$.)

As for your example, there is no function $f(x)=\sqrt{x}$ with domain and codomain the natural numbers. For each element of the domain, the function is required to give an output which is in the codomain. Since $\sqrt{5}$ is not a natural number, the rule $f(x)=\sqrt{x}$ does not define a function with domain the natural numbers and codomain the natural numbers.

0
On

Functions have a domain and a range. The domain is the input(s) into the function and the range is the output of the function. The domain could be natural numbers and the range could be real numbers, for instance.

2
On

Yes and no.

Yes, a function can map from any set $A$ to any set $B$ ($f:A\to B$). The sets don't have to have any intrinsic relations to each other. So we can easily have $f: \mathbb R\to \mathbb N$ or from $g:\mathbb N \to \mathbb R$ or $h:\{$mathetical symbols$\} \to \{$elephants whose middle name is Fred$\}$. (Ex: $h(+) = $Tantor Fred Bigguy of the Santa Fe Zoo; $f(\sum)= $Babar Fred Gibbs of the Indonesian Art Institute....)$

No.

To be a function $f:A\to B$ we must have that for all $a \in A$ then $f(a)$ is a well-defined unambiguous element of $B$.

In your example $f: \mathbb N to \mathbb N$ via $f(n) =\sqrt{n}$ is not a function because if $n \in \mathbb N$ but $n$ is not a perfect square then $f(n)$ is not well-defined and unambiguous.

However $f: \{1,4,9,.....|k^2 \in \mathbb N\} \to \mathbb N$ via $f(x) = \sqrt x$ is a well defined function.

As is $f:\mathbb N \to \mathbb R$ via $f(x) =\sqrt x$.

0
On

Presented abstractly, a function $f:X\to Y$, for two non-empty sets $X,Y$ is a subset $F$ of $ X\times Y$ such that for each $x\in X$ there exists a unique $z\in F$ such that $z=(x,y)$, some $y\in Y$. In words, a function is any assignment of precisely one $y\in Y$ to every $x\in X$. Note that this is a reasonable definition of an abstract input/output model.

Let us check your example. You define $f:\mathbb N\to\mathbb N$ by $F=\{(n, \sqrt{n}): n\in\mathbb N\}$. But $F\not\subset \mathbb N\times \mathbb N$, so to make it a function you need to either enlarge the outputs and/or remove some inputs. For a data-type example, if your function is $$f:\text{Integers} \to \text{Strings},\quad\text{ defined by }F=\{(n,\text{'n'}): n\in \text{Integers}\}\subset \text{Integers}\times \text{Strings}$$ then the $F$ is a well defined function. Conversely, if you define $$\tilde f:\text{Integers} \to \text{Strings},\quad\text{ defined by }\tilde F=\{(\text{'n'},\text{n}): \text{'n'}\in \text{Strings}\}\not\subset \text{Integers}\times \text{Strings}$$ as $$\text{Strings}\ni\text{'helloworld!'}\mapsto \text{helloworld!}\not\in \text{Integers},$$ hence $\tilde F$ is not a function. Possible answer: given two sets $X,Y$, as long as you respect the definition of a function, anything goes (let me know if the notation I use is confusing).