Functions are blocks of code that can be reused simply by calling the function. This enables simple, elegant code reuse without explicitly re-writing sections of code. This makes code both more readable, makes for easier debugging, and limits typing errors.
Functions in R are created using the `function` keyword, along with a function name and function parameters inside parentheses.
The `return()` function can be used by the function to return a value, and is typically used to force early termination of a function with a returned value.
Alternatively, the function will return the final printed value.
```{r}
# return a value explicitly or simply by printing
sum = function(a, b){
c = a + b
return(c)
}
sum = function(a, b){
a + b
}
result = sum(1, 2)
# result = 3
```
You can also define default values for the parameters, which R will use when a variable is not specified during function call.
```{r}
sum = function(a, b = 3){
a + b
}
result = sum(a = 1)
# result = 4
```
You can also pass the parameters in the order you want, using the name of the parameter.
```{r}
result = sum(b=2, a=2)
# result = 4
```
R can also accept additional, optional parameters with '...'
```{r}
sum = function(a, b, ...){
a + b + ...
}
sum(1, 2, 3) #returns 6
```
Functions can also be run anonymously. These are very useful in combination with the 'apply' family of functions.
```{r}
# loop through 1, 2, 3 - add 1 to each
sapply(1:3,
function(i){
i + 1
})
```
### Notes
- If a function definition includes arguments without default values specified, values for those values must be included.
```{r}
sum = function(a, b = 3){
a + b
}
sum(b = 2) # Error in sum(b = 2) : argument "a" is missing, with no default
```
- Variables defined within a function only exist within the scope of that function, but will check larger environment if variable not specified
```{r}
double = function(a){
a * 2
}
double(x) # Error in double(x) : object 'x' not found
double = function(){
a * 2
}
a = 3
double() # 6
```
## In-built functions in R
- R comes with many functions that you can use to do sophisticated tasks like random
sampling.
- For example, you can round a number with the `round()`, or calculate
its factorial with the `factorial()`.
```r
> round(4.147)
[1] 4
> factorial(3)
[1] 6
> round(mean(1:6))
[1] 4
```
- The data that you pass into the function is called the function’s argument.
- You can simulate a roll of the die with R’s `sample()`function. The `sample()` function takes two arguments: a vector named `x` and a number named `size`. For example: