# Example: Creating a manual version of the 'mean' function
new_mean <- function(values) {
# The 'Body'
result <- sum(values) / length(values)
# The 'Return' (Output)
return(result)
}
# Test it out
x <- 1:5
new_mean(x)[1] 3

Writing functions is the best way to keep your code DRY (Don’t Repeat Yourself). If you find yourself copy-pasting code three times, it’s time to write a function.
A function has three parts:
Input (Arguments): What the function needs to work (e.g., a vector of numbers).
Body: The actual code/math being performed.
Output (Return): What the function gives back to you.
# Example: Creating a manual version of the 'mean' function
new_mean <- function(values) {
# The 'Body'
result <- sum(values) / length(values)
# The 'Return' (Output)
return(result)
}
# Test it out
x <- 1:5
new_mean(x)[1] 3
return()
In R, the function will automatically return the very last line of code executed inside the braces. However, using the explicit return() function is a “Best Practice” because it makes it much clearer to other researchers exactly what your function is providing.