How to write Functions in R

In this tutorial we look at how to write functions in R.

What are functions?

Functions are user defined pieces of code that perform a desired operation on given input(s) and return the output back to the user. The main characteristics of functions are:

  1. They contain lines of code enclosed within a body. The variable defined and declared inside the body are not available outside the body unless especially specified by a return statement. Example
  2. In R, an explicit return statement is not required. The last variable evaluated is returnedExample
  3. The function definition defines one or more variables that can be passed by the caller of the function.Example
  4. The function can return an object or a list.Example
  5. The variables passed to the R function are normally passed by value. i.e. a copy of the object is passed, so if you change the object inside the function, the change is not reflected outside the function.Example
  6. The R.oo package provides ways to pass arguments by values.
  7. R follows principles of functional programming. You can assign functions to a variable and pass functions as arguments to other functions.Example
  8. R functions do not require a name. They can be anonymous.Example
  9. R supports closures. closures are function that can access variables from its enclosing environment.Example

Example – function without arguments and scope

The basic format of writing a function is

In this example we create a function that does not accept any arguments. The function prints todays date. The example also creates a local variable and demonstrates how that local variable is not accessible outside the function.

The variable todaysDate is defined inside the function body and is therefore not available outside the function.

Example – No explicit return, last variable returned

In this example we have written a function that creates today’s date. The function has no explicit return statement but evaluates the todaysDate variable as the last statement. The function therefore returns that value.

An explicit return() is also possible, but its slightly faster if you don’t use a return statement. Of course, some developers may find code more readable if it there is a return statement.

Examples – Functions with parameters

In the example above we saw a function that takes in two parameters a and b. Lets look at some other variations.

  • Functions having parameters with default value.

    In the example below we did not specify b since it has a default value of 1.

  • Calling a function with named arguments

    In the example above we call the function by specifying the name of the arguments. Note that when we specify names the order is no longer important. You can also do this:

    we specified the name of only one argument, R figured out the other argument. Here’s an interesting case

    We specified b by name and then two variables without name and R assigned them.

Example – return object or list

Here’s a really dumb function that just returns a data frame.

and another that returns a list (of the data frame that we created above.)

Example – pass by value.

We create a variable a and assign 2 to it. we modified that variable and changed it to 4 however when we exit the function a is still 2. Works for a list too

Example – Creating and passing functions

We create a slightly simplistic example to explain passing functions.

Step 1 is the create two functions

The function c takes in 3 arguments. The first is a ‘function’, the next two are numbers

Example – Anonymous function

In this example we demonstrate an anonymous function. We continue with the example above, but instead of passing in a function we create a function

We created a function inline and passed it the func. The function does not have a name.

Leave a Reply

Your email address will not be published. Required fields are marked *