11.4 Summary

ds4psy: (11) Functions

This chapter did not discuss any new tidyverse package, but rather two basic notions of computer programming:

  • A function is defines a particular mapping from inputs to outputs. When creating more complex programs, defining functions allows us to abstract procedures that we may want to use repeatedly.

  • Writing functions involves controlling the flow of information, both between and within functions. Conditional statements (aka. if-then statments) define criteria for distinguishing cases and then use them for pursuing alternative paths in our programs.

After working through this chapter, you are able to

  1. explain what functions are and why they are useful,
  2. use base R to define new functions,
  3. describe and check functions,
  4. control the flow of information by using conditional statements.

Take a look at the RStudio cheatsheets on Base R and Advanced R on Functions to check which commands you are now familiar with and which others you can still discover in the future:

Summary on functions from the [RStudio cheatsheet](https://www.rstudio.com/resources/cheatsheets/) on **Advanced R**.

Figure 11.2: Summary on functions from the RStudio cheatsheet on Advanced R.

Let’s test the functionality of our knowledge and skills on functions by completing the following exercises.