R - functions

oddcount <- function="function" p="p" x="x"># defining a function, oddcount(), with one argument, x. Left brace demarcates the start of the body of the function
k <- 0="0" br="br"> for (n in x) {
if (n %% 2 == 1) k <- br="br" k="k"> }
return(k)
}
# Useage
oddcount(c(1,3,5))
[1] 3
R also makes frequent use of default arguments. In the (partial) function definition
function(x,y=2)
y will be initialized to 2 if the programmer does not specify y in the call.
f <- br="br" c="c" function="function" return="return" x="x"> f(1:3,0)
[1] 1 4 9
> f(1:3,1)
[1] 4 9 16

Example of the use of superassignment
> w <- 5="5" br="br"> > addone <- br="br" function="function" x="x"> + x <- br="br" x="x"> > addone(w) # formal argument x is only a local copy of w
> w # so w doesn’t change
[1] 5
> addone <- br="br" function="function" return="return" x="x"> > w <- addone="addone" br="br" w="w"> > w
[1] 6
> addone <- br="br" function="function" of="of" op="op" superassignment="superassignment" use="use" w="w"> > addone()
> w
[1] 7