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
}

Return

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)
30
> 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

Learn to be a Pythonista!

Learn Python in One Day

Learn to be a Pythonista! Try Python

Have you always wanted to learn computer programming but are afraid it'll be too difficult for you? Or you're familiar with some programming but are interested in learning Python fast? Then this book is for you. You no longer have to waste your time and money learning Python from lengthy books, expensive online courses or complicated Python tutorials.

$ Check price
11.9711.97Amazon 4 logo(185+ reviews)

More Python resources

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Ad