11.5 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,
  5. recognize some advanced issues (like recursion, sorting algorithms, and measuring the performance of functions).

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 on Advanced R.

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

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