You are correct, in that a function must be defined for every element in its domain, and that therefore you cannot create a function $f:\mathbb{R}\to\mathbb{R}$ defined by $f(x)=\frac{1}{x}$, because $\frac{1}{0}$ is undefined.
You can however create a function $g:\mathbb{R}\setminus\{0\}\to\mathbb{R}$ defined by $g(x)=\frac{1}{x}$, because we have removed the problematic element $0$ from the domain.
You can also create a function $h:\mathbb{R}\to \mathrm{P}^1(\mathbb{R})$ defined by $h(x)=\frac{1}{x}$, where $\mathrm{P}^1(\mathbb{R})=\mathbb{R}\cup\{\infty\}$ is the real projective line (Wikipedia), which has the element $\infty$ available to be the output of $h(0)$.