Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 156
Example: A bespoke function
Оглавление# c_surface # Calculates the surface of a circle # Arguments: # radius -- numeric, the radius of the circle # Returns # the surface of the cicle c_surface <- function(radius) { x <- radius ∧ 2 * pi return (x) } c_surface(2) + 2 ## [1] 14.56637
Note that it is not necessary to explicitly “return” something. A function will automatically return the last value that is send to the standard output. So, the following fragment would do exactly the same:
# c_surface # Calculates the surface of a circle # Arguments: # radius -- numeric, the radius of the circle # Returns # the surface of the cicle c_surface <- function(radius) { radius ∧ 2 * pi } # Test the function: c_surface(2) + 2 ## [1] 14.56637