[Tex/LaTex] Missing number, treated as zero when using \If without \EndIf with algpseudocode

algpseudocode

I'm trying to typeset a pseudo code with an \If block without the matching \EndIf using algpseudocode. This is my MWE:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

    % Insert the algorithm
    \begin{algorithm}
        \caption{Compute some function by recursive algorithm}
        \begin{algorithmic}[1]
            \Function{$some$}{m,n}
            \If{$n=1$ \textit{or} $m=1$}
            \State \Return 1
            \Else
            \State \Return $some(n,m-1)+some(n-1,m)+some(n-1,m-1)$
            \EndFunction
        \end{algorithmic}
    \end{algorithm}
\end{document}

But I get the error: missing number, treated as zero. What do I do to make the \If code without the matching \EndIf?

Best Answer

The error is because you're missing and \EndIf.

The algpseudocode package relies on the \If...\EndIf structure (like any decent language should) to keep track of the indentation of the code.

If you don't want to show the \Ends, you can use the noend option for algpseudocode. Nonetheless you have to use the \EndIf for algpseudocode's internal bookkeeping.

enter image description here

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}
    % Insert the algorithm
    \begin{algorithm}
        \caption{Compute some function by recursive algorithm}
        \begin{algorithmic}[1]
            \Function{$some$}{m,n}
            \If{$n=1$ \textit{or} $m=1$}
            \State \Return 1
            \Else
            \State \Return $some(n,m-1)+some(n-1,m)+some(n-1,m-1)$
            \EndIf % <- Here
            \EndFunction
        \end{algorithmic}
    \end{algorithm}
\end{document}