# Function numbering in algorithms

Algorithms are getting numbered automatically but unable to get same for the functions.

In the following example, Algorithms are numbered as Algorithm 1 and Algorithm 2 by themselves but Functions didn't. Please advice.

\documentclass{ucbthesis}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{epstopdf}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{tikz,pgfplots,pgfplotstable}
\usepackage{tikz-qtree}
\usepackage{forest}
\usepackage{lettrine}
\usepackage{mathtools}
\usepackage{lscape}
\usepackage{pgf-pie}
\usepackage{caption}
\usepackage{dashbox}
\usepackage{mathptmx}
\usepackage{xcolor}
\usepackage{hyperref}
\begin{document}
\begin{algorithm}
\caption{First Algorithm}
\For{$j \in V$}{
}
\end{algorithm}

\begin{algorithm}
\caption{Second Algorithm}
\For{$j \in V$}{
}
\end{algorithm}

\begin{function}
\label{function1}
\caption{First Function ()}
\For{$k \in R$}{

}
\end{function}

\begin{function}
\label{function2}
\caption{Second Function ()}
\For{$k \in R$}{

}
\end{function}

I am refering to function \ref{function1} and function \ref{function2}

\end{document}

What you need is the package option procnumbered, which “makes the procedure and function to be numbered as algorithm”. See Page 19 of the algorithm2e manual.

Added: In order to get the cross-reference right, you need to place \label after \caption as @egreg mentioned in the comments below. See also this short explanation by @PhilMiller and this longer explanation by @anon.

\documentclass{ucbthesis}
\usepackage[ruled,vlined,linesnumbered,procnumbered]{algorithm2e}
\usepackage{newtxtext}
\usepackage{newtxmath}

\begin{document}
\begin{algorithm}
\caption{First Algorithm}
\For{$j \in V$}{
}
\end{algorithm}

\begin{algorithm}
\caption{Second Algorithm}
\For{$j \in V$}{
}
\end{algorithm}

\begin{function}
\caption{First Function ()}
\label{function1}
\For{$k \in R$}{
}
\end{function}

\begin{function}
\caption{Second Function ()}
\label{function2}
\For{$k \in R$}{
}
\end{function}

I am refering to Function~\ref{function1} and Function~\ref{function2}.

\end{document}

In case you want your functions to be numbered independently, then you need to set up a new counter for the function environment:

\documentclass{ucbthesis}
\usepackage[ruled,vlined,linesnumbered,procnumbered]{algorithm2e}
\usepackage{newtxtext}
\usepackage{newtxmath}
% Set up a new counter for function
\newcounter{function}
\usepackage{etoolbox}
% Change the original counter to the new counter
\makeatletter
\AtBeginEnvironment{function}{%
\let\c@algocf\c@function
}
\makeatother

\begin{document}
\begin{algorithm}
\caption{First Algorithm}
\For{$j \in V$}{
}
\end{algorithm}

\begin{algorithm}
\caption{Second Algorithm}
\For{$j \in V$}{
}
\end{algorithm}

\begin{function}
\caption{First Function ()}
\label{function1}
\For{$k \in R$}{
}
\end{function}

\begin{function}
\caption{Second Function ()}
\label{function2}
\For{$k \in R$}{
}
\end{function}

I am refering to Function~\ref{function1} and Function~\ref{function2}.

\end{document}