# Why do I get the error “! Illegal parameter number in definition of \appendix.”while redefining the \appendix command?

I'm trying to redefine the \appendix command so that my appendix headings go to the top of the pages. To do this, I tried the following code, but I get the error ! Illegal parameter number in definition of \appendix. Why am I getting this error and how to get rid of it?

\documentclass{book}
\makeatletter
\renewcommand\appendix{\par
\vspace*{0\p@}%
{\parindent \z@ \raggedright \normalfont
\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\huge\bfseries \@chapapp\space \thechapter
\par\nobreak
\vskip 20\p@
\fi
\fi
\interlinepenalty\@M
\Huge \bfseries #1\par\nobreak
\vskip 40\p@
}}
\setcounter{chapter}{0}%
\setcounter{section}{0}%
\gdef\@chapapp{\appendixname}%
\gdef\thechapter{\@Alph\c@chapter}}
\makeatother
\begin{document}
\appendix
\chapter{Foo}
\end{document}


Please keep in mind that, I know that a solution is to put the redefinition of the \@makechapterhead at the beginning of the \appendix command in the document body, but I want to do the job in the preamble. Also, I'd prefer not to use extra packages to do the job.

-

Inside the definition of \appendix, #1 would be the first parameter of \appendix that does not exists, \appendix is defined without parameters. For the parameters of a definition inside a definition you need to double the hash char:

\renewcommand\appendix{\par

If \@makechapterhead contained another definition with a parameter, then #### would be needed for the inner parameter.