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

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

Learn to be a Pythonista!

Introducing Python

Learn to be a Pythonista! Try Python

Easy to understand and fun to read, Introducing Python is ideal for beginning programmers as well as those new to the language. Author Bill Lubanovic takes you from the basics to more involved and varied topics, mixing tutorials with cookbook-style code recipes to explain concepts in Python 3. End-of-chapter exercises help you practice what you learned.

$ Check price
39.9939.99Amazon 4.5 logo(37+ reviews)

More Python resources