03. Functions

Functions are chunks of reusable code that take inputs, use them to compute some value, then returns a result. Writing functions are good practice since they are able to save you time and keeps your code DRY (don't repeat yourself).

Declaring a function

Creating a function is like declaring a variable, except with the function keyword, followed by any parameters that the function may use within parentheses.

> sumAndDouble <- function(x) {
  return sum(x) * 2


The return keyword is used to return the resulting value. This value will replace where the user made the function call.

If no return keyword is used, the last value calculated will be returned.

Formal parameter vs. actual argument

We say that x in function(x) is the formal argument or formal parameter of the function, while the actual argument is the actual value passed in. Whatever actual argument is passed in gets stored into the formal parameter for use within the function.

Formal vs. Actual parameters.
Formal vs. Actual parameters.

Functions in RStudio

To create functions in RStudio, create a new script (the + button on the top-left corner) and type in your function. Hit the "Source" button when you're ready to create the function in your environment.

Writing up functions in the script panel of RStudio.
Writing up functions in the script panel of RStudio. Make sure to hit "Source" to run the script and create the function object in your Environment.

Setting default arguments

In most functions, variable are assigned a default value. To set a default value, write it in within the formal argument like so:

sumAndDouble <- function(x, multiplyBy=2) {
  return sum(x) * muliplyBy

The default value of 2 will take hold when no only one argument is entered. However, we can overwrite this value by simply passing in another argument.

> sumAndDouble(a)
[1] 30
> sumAndDouble(a, 3)
[1] 45

Variable Scope

Scope is the term used to describe the range at which a variable may be accessed. There are two types of scope - local and global.

Local scope

Local scope refers to variables declared within functions. They disappear once the function is done executing. Thus, if our function declared some value val, it wouldn't be accessible outside the function sumAndDouble.

sumAndDouble <- function(x, multiplyBy=2) {
  val <- sum(x) * multiplyBy
  return val
> sumAndDouble(c)
> val
Error: object 'val' not found

Global scope

On the other hand, global scope covers variables that are created outside functions and are accessible everywhere in your current session.

To declare a global variable within a function, use the superassignment operator, <<-.

sumAndDouble <- function(x, multiplyBy=2) {
  val <<- sum(x) * multiplyBy
  return val

Now we are able to access the val variable from outside the function.

> sumAndDouble(a)
[1] 30
> val
[1] 30

Become a Bioinformatics Whiz!

Introduction to Bioinformatics Vol. 2

Become a Bioinformatics Whiz! Try Bioinformatics

This is Volume 2 of Bioinformatics Algorithms: An Active Learning Approach. This book presents students with a light-hearted and analogy-filled companion to the author's acclaimed course on Coursera. Each chapter begins with an interesting biological question that further evolves into more and more efficiently solutions of solving it.

$ Check price
49.9949.99Amazon 5 logo(5+ reviews)

More Bioinformatics resources

Learn to be a Pythonista!

Learning Python

Learn to be a Pythonista! Try Python

Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz's popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It's an ideal way to begin, whether you're new to programming or a professional developer versed in other languages.

$ Check price
64.9964.99Amazon 4 logo(279+ reviews)

More Python resources