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!

Programming Python

Learn to be a Pythonista! Try Python

Programming Python shows in-depth tutorials on the language's number of application domains including: system administration, GUIs, the Web, networking, front-end scripting layers, and more. This book focuses on commonly used tools and libraries to give you a comprehensive understanding of Python’s many roles in practical, real-world programming.

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

More Python resources

Take your Linux skills to the next level!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Ad