[Rd] using function names as parameters (PR#469)

Peter Dalgaard BSA
Fri, 03 Mar 2000 11:55:21 -0000

hothorn@ci.tuwien.ac.at writes:

> When using a function name as parameter, here date, the following happends:
>
> R> foo <- function(date=date()) cat(date, "\n")
> R> foo()
> Error in cat(date, "\n") : recursive default argument reference

This is not a bug. Default arguments are evaluated in the frame of the
called function, so when the date variable is requested and no actual
argument was supplied, R tries to evaluate the default expression, but
that involved the variable "date" again. If you pass an argument, the
default expression is never evaluated and the problem does not arise.

Similarly a definition like

function(x=y, y=x) x + y

is asking for trouble if both x and y are left unspecified on the call.

[This has nothing to do with passing functions as parameters, by the
way]
--
   O__  ---- Peter Dalgaard             Blegdamsvej 3
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907