function
Try it yourself →Defines your own command.
Syntax
name <- function(params) { ... return(...) }Description
Defined with function and assigned to a variable. return(x) returns the result (with parentheses); the last expression is also returned automatically.
Example
square <- function(x) {
return(x * x)
}
cat(square(5), "\n", sep="")Expected output
25
When to use?
Preventing code repetition, breaking the program into parts.