Prueba asi:
Código PHP:
function agregar($p, $nom)
{
echo '<form name="ag" method="post" action="carrocomp.php">';
echo '<input type="hidden" name="boton1" value="Agregar">';
echo '<input type="hidden" name="prec" value="' . $p . '">';
echo '<input type="hidden" name="nomb" value="' . $nom . '">';
echo '<button name="agree">Agregar</button>';
echo '</form>';
}