A big part of understanding how R works can be done by figuring out how R functions work. After going through the previous chapters, you should know the most commonly used basic functions. However, you may still be confused about their exact behavior. Suppose we create the following function:
test0 <- function(x, y) { if (x > 0) x else y }
The function is somewhat special because y
seems to be needed only when x
is greater than zero. What if we only supply a positive number to x
and ignore y
? Will the function fail because we don't supply every argument in its definition? Let's find out by calling the following function:
test0(1) ## [1] 1
The function works without y
being supplied. It looks like we are not required to supply the values to all arguments when we call a function but only to those that are needed. If we call test0
with a negative number, y
is needed:
test0(-1) ## Error in test0(-1): argument "y" is missing,...