Skip to content

using function names as parameters (PR#469)

2 messages · Torsten Hothorn, Peter Dalgaard

#
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

R> foo("Good Morning!")
Good Morning!

works.

Torsten

--please do not edit the information below--

Version:
 platform = i686-unknown-linux
 arch = i686
 os = linux
 system = i686, linux
 status = 
 major = 1
 minor = 0.0
 year = 2000
 month = February
 day = 29
 language = R

Search Path:
 .GlobalEnv, Autoloads, package:base

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
hothorn@ci.tuwien.ac.at writes:
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]