Function Basics
In this page:
Function Basics
A function in R is created with function(args) { body } and assigned to a name like any other value, since functions are first-class objects in R. Calling it looks just like calling any built-in function. Breaking logic into functions is the primary way to organize and reuse R code.
Note: Functions are ordinary values in R -- you can pass them as arguments to other functions, just like numbers or strings.
Example: Function Basics
greet <- function() {
cat("Hello from a function!\n")
}
greet()
greet()
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: