function                package:base                R Documentation

_F_u_n_c_t_i_o_n _D_e_f_i_n_i_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     These functions provide the base mechanisms for defining new
     functions in the R language.

_U_s_a_g_e:

     function( arglist ) expr
     return(value)

_A_r_g_u_m_e_n_t_s:

 arglist: Empty or one or more name or name=expression terms.

   value: An expression.

_D_e_t_a_i_l_s:

     The names in an argument list can be back-quoted non-standard
     names (see 'backquote').

     If 'value' is missing, 'NULL' is returned.  If it is a single
     expression, the value of the evaluated expression is returned.
     (The expression is evaluated as soon as 'return' is called, in the
     evaluation frame of the function and before any 'on.exit'
     expression is evaluated.)

     If the end of a function is reached without calling 'return', the
     value of the last evaluated expression is returned.

_W_a_r_n_i_n_g:

     Prior to R 1.8.0, 'value' could be a series of non-empty
     expressions separated by commas.  In that case the value returned
     is a list of the evaluated expressions, with names set to the
     expressions where these are the names of R objects.  That is,
     'a=foo()' names the list component 'a' and gives it the value
     which results from evaluating 'foo()'.

     This has been deprecated (and a warning is given), as it was never
     documented in S, and whether or not the list is named differs by S
     versions.  Supply a (named) list 'value' instead.

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_. Wadsworth & Brooks/Cole.

_S_e_e _A_l_s_o:

     'args' and 'body' for accessing the arguments and body of a
     function.

     'debug' for debugging; using 'invisible' inside 'return(.)' for
     returning _invisibly_.

_E_x_a_m_p_l_e_s:

     norm <- function(x) sqrt(x%*%x)
     norm(1:4)

     ## An anonymous function:
     (function(x,y){ z <- x^2 + y^2; x+y+z })(0:7, 1)

