---
title: "STAT 331 Week 7 Day 1 Handout"
format: html
embed-resources: true
---


1. Write a function named `add_two()` that will add `2` to whatever number is input. 

```{r}




```


2. In your function what is the:

- Function Name:
- Function Argument(s):
- Function Body:
- Function Return:


3. What if we wanted to write a more general function, named `add_something()`. 

The function would take **two** inputs: 

1. `x` the vector to add to
2. `something` the value to add to `x`

How would your function change?

```{r}




```

4. Are your arguments ***optional*** or ***required***?

5. What is the difference? 

6. What are the two ways you can return a value in a function?

7. Which method for returning a value makes more sense to you?

8. Add input validation to your `add_something` function using `stopifnot`. Look at the documentation to see how to add a helpful message.

## Function Practice

9. Fill in the code below to create a function named `above_average()`. The function should keep only the elements of `x` greater than the mean.

```{r}
above_average <- function(x) {

  # Step 1: Compute mean of x
  
  
  # Step 2: Subset x to keep only values > mean
  
  
  # Step 3: Return the result
  
  
}
```


10. Write down the steps you would need to create a function named `every_third()` that takes in a vector and returns every third element from that vector (i.e., indices 1, 4, 7, 10, etc.). 

**Think about:**

- What inputs the function should take.
- How to identify which positions in the vector are "every third."
- How to select those elements from the vector.

