Skip to content
Justice Tech School

R Functions

Create a function with function(). The last expression is returned. Parameters can have defaults, and ... accepts extras.

Example
add <- function(a, b = 1) { a + b }
print(add(2)); print(add(2, 5))
sq <- function(x) x^2
print(sq(6))

Exercise

Define a function.

add <- (a, b) a + b

Quiz Test

How does an R function return a value?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.