This creates a function named myFunction with no parameters.
myFunction <- function() {
print("Hello from a function!")
}Invokes the function and runs the code inside.
myFunction()You can pass arguments or use default values if omitted.
return() sends back a result. If omitted, the last line is returned.
x <- 10
test <- function() {
x <- 5
print(x)
}
test() # prints 5
print(x) # prints 10Local variables inside a function do not affect global variables unless <<- is used.
lapply(1:5, function(x) x^2)Used for short, one-off operations inside other functions.
Ask the AI if you need help understanding or want to dive deeper in any topic