R Functions

In simple terms, a function is a block of statements that can be used repeatedly in a program.

R provides many built-in functions and allows programmers to define their own functions.

Syntax

Here’s the syntax of a function in R:

r function syntax

Function Name is an identifier by which the function is called

Arguments contains a list of values passed to the function

Function Body is executed each time the function is called

Return keyword ends function call and sends data back to the program

Create a Function

To define a function in R, use the function command and assign the results to a function name.

Example: Create a function ‘myfunc’

> myfunc <- function() {
+   print('Hello, World!')
+ }

If you have only one statement to execute, you can skip curly braces.

Example: Write a function in one line

> myfunc <- function() print('Hello, World!')

Call a Function

You can call (run) the function by adding parentheses after the function’s name.

Example:

> myfunc <- function() {
+   print('Hello, World!')
+ }

> myfunc()
[1] "Hello, World!"

Pass Arguments

You can send information to a function through arguments.

Arguments are declared after the function keyword in parentheses.

You can send as many arguments as you like, just separate them by a comma ,.

Example:

> sum <- function(x, y) {
+   x + y
+ }

> sum(2, 3)
[1] 5

Named Arguments

If you pass arguments to a function by name, you can put those arguments in any order.

Example:

> pow <- function(x, y) {
+   x ^ y
+ }

> # using argument names
> pow(x=2, y=3)
[1] 8

> # changing the order
> pow(y=3, x=2)
[1] 8

Default Argument Value

You can assign a default value to an argument.

So, when you call the function without argument, it uses the default value.

Example: Set default value ‘3’ to second argument

> pow <- function(x, y=3) {
+   x ^ y
+ }

> # function will use default y value
> pow(2)
[1] 8

> # specifying a different y value
> pow(2, 4)
[1] 16

Return a Value

To return a value from a function, simply use a return() function.

Example: Return a value using return function

> sum <- function(x, y) {
+   return(x + y)
+ }

> sum(2, 3)
[1] 5

If you do not include any return() function, it automatically returns the last expression.

Example: Return the value of the last expression in the body

> sum <- function(x, y) {
+   x + y
+ }
> 
> sum(2, 3)
[1] 5

Return Multiple Values

You can return multiple values by saving the results in a vector (or a list) and returning it.

Example:

> math <- function(x, y) {
+   add <- x + y
+   sub <- x - y
+   mul <- x * y
+   div <- x / y
+   c(addition = add, subtraction = sub,
+     multiplication = mul, division = div)
+ }

> math(6, 3)
      addition    subtraction multiplication       division 
             9              3             18              2

Lazy Evaluation

R functions perform lazy evaluation that dramatically extends the expressive power of functions.

It is the technique of not evaluating arguments unless and until they are needed in the function.

Example:

> myfunc <- function(x, y) {
+   if(!x){
+     return(y)
+   }
+   else{
+     return(x)
+   }
+ }

> # y is not evaluated so not including it causes no harm
> myfunc(6)
[1] 6

> # y is evaluated so not including it raises error
> myfunc(0)
Error in myfunc(0) : argument "y" is missing, with no default

Variable Length Argument

In R, it is often convenient to accept a variable number of arguments passed to the function.

To do this, you specify an ellipsis (...) in the arguments when defining a function.

It accepts variable number of arguments, in the sense that you do not know beforehand how many arguments can be passed to your function by the user.

For example, below function prints the first argument and then passes all the other arguments to the summary() function.

Example:

> myfunc <- function(x,...) {print(x); summary(...)}
> v <- 1:10

> myfunc("Summary of v:", v)
[1] "Summary of v:"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    3.25    5.50    5.50    7.75   10.00 

You can also directly refer to the arguments within the argument list (...) through the variables ..1, ..2, to ..9.

For example, ..1 refers to the first argument, ..2 refers to the second, and so on.

Example:

> myfunc <- function(...) {cat(..1, ..2)}

> myfunc("Hello", "World!")
Hello World!

It is also possible to read the arguments from the argument list by converting the object (...) to a list within the function body.

For example, below function simply sums all its arguments:

Example:

> addAll <- function(x,...) {
+   args <- list(...)
+   for (a in args) x <- x + a
+   x
+ }

> addAll(1,2)
[1] 3

> addAll(1,2,3,4,5)
[1] 15