How to type function range such as f: Z to N [duplicate]

I am new to latex and I am having trouble typing the function range before I define the function. So I am trying to define a piecewise function, and how would I have something like this?

marked as duplicate by Werner math-mode StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Jan 26 '18 at 7:10

• Welcome! Please always provide an MWE for your question. Assuming that you load the amssymb package, the code is $f:\mathbb{N}\mapsto\mathbb{N}$, but you should really use \to instead of \mapsto here. – user121799 Jan 26 '18 at 4:59
• Hi, for some reason, it keeps giving me undefined control sequence when I use mathbb. – catie Jan 26 '18 at 5:02
• add \usepackage{amssymb} to your preamble – koleygr Jan 26 '18 at 5:04
• I posted a code below. Please tell me when you read and understood it, I'll erase it then. – user121799 Jan 26 '18 at 5:18

Thanks, Mico!

\documentclass{article}
\usepackage{amsmath,amssymb}
\begin{document}
$f\colon\mathbb{Z}\to\mathbb{N}$ is more correct than
$f\colon\mathbb{Z}\mapsto\mathbb{N}$.
\begin{align}
f\colon\mathbb{Z}&\to\mathbb{N}\notag\\
n&\mapsto \left( n\mod 3\right)^2
\end{align}
\end{document}


• Please change all three instances of : to \colon. (The former is a relational operator.) – Mico Jan 26 '18 at 5:30
• @Mico: Will do ... but I did not intend to keep this post. – user121799 Jan 26 '18 at 5:31