Chapter 26 Syntax of a function: function()
<- function(argument1, argument2, ...) {
function_name # What we want the function to do
body return(values) # Optional
}
function_name
is the name of the function, and will be stored in theR
environemnt as an object with this name;argument
s take the defined values that can be used within the function;body
contains the statements that define what the function does;output
contains the returned value from the function. Ifreturn()
is absent, then the last expression is returned.
26.1 Arguments of a function()
<- function(argument1, argument2, ...) {
function_name # What we want the function to do
body return(values) # Optional
}
Arguments are the input values of your function and will have the information your function needs to be able to perform correctly.
A function can have between zero and an infinity of arguments. See the following example:
<- function(number1, number2, number3) {
operations <- (number1 + number2) * number3
result print(result)
}
operations(1, 2, 3)
## [1] 9
Challenge 4
Using what you learned previously on flow control, create a function print_animal()
that takes an animal
as argument and gives the following results:
<- "dog"
Scruffy <- "cat"
Paws print_animal(Scruffy)
## [1] "woof"
print_animal(Paws)
## [1] "meow"
Challenge 4: Solution
Using what you learned previously on flow control, create a function print_animal()
that takes an animal
as argument and gives the following results:
<- "dog"
Scruffy <- "cat"
Paws print_animal(Scruffy)
## [1] "woof"
print_animal(Paws)
## [1] "meow"
<- function(animal) {
print_animal if (animal == "dog") {
print("woof")
else if (animal == "cat") {
} print("meow")
} }
26.2 Default argument values in a function
Arguments can be provided with a default value, or even be optional.
Default values are useful when using a function with the same settings. The flexibility to depart from default values is still there, if needed.
<- function(number1, number2, number3 = 3) {
operations <- (number1 + number2) * number3
result print(result)
}operations(number1 = 1, number2 = 2, number3 = 3)
## [1] 9
# is equivalent to
operations(1, 2)
## [1] 9
operations(1, 2, 2) # we can still change the value of number3 if needed
## [1] 6
26.3 The ellipsis argument: ...
The special argument ...
allows you to pass arguments from other undefined functions, i.e. allowing for an indefinite number of arguments inputs.
<- function(...) {
paste_anything_fun <- list(...)
arguments paste0(arguments)
}
paste_anything_fun("I", "want", "a break!")
## [1] "I" "want"
## [3] "a break!"
<- function(x, mult = 100, ...) {
percentages <- round(x * mult, ...)
percent paste(percent, "%", sep = "")
}
percentages(c(0.543, 0.534, 0.466))
## [1] "54%" "53%" "47%"
# ?round
percentages(c(0.543, 0.534, 0.466), digits = 2)
## [1] "54.3%" "53.4%" "46.6%"
The special argument ...
allows you to pass on arguments to another function used inside your function. Here we use ...
to pass on arguments to plot()
and points()
.
<- function(CO2, ...) {
plot.CO2 plot(x = CO2$conc, y = CO2$uptake, type = "n", ...)
for (i in 1:length(CO2[, 1])) {
if (CO2$Type[i] == "Quebec") {
points(CO2$conc[i], CO2$uptake[i], col = "red", type = "p",
...)else if (CO2$Type[i] == "Mississippi") {
} points(CO2$conc[i], CO2$uptake[i], col = "blue",
type = "p", ...)
}
} }
plot.CO2(CO2, cex.lab = 1.2, xlab = "CO2 concentration", ylab = "CO2 uptake")
plot.CO2(CO2, cex.lab = 1.2, pch = 20, xlab = "CO2 concentration",
ylab = "CO2 uptake")
26.4 Return values
The last expression evaluated in a function
becomes the return value:
<- function(x) {
myfun if (x < 10) {
0
else {
} 10
}
}myfun(5)
## [1] 0
myfun(15)
## [1] 10
function()
itself returns the last evaluated value even without including return()
26.5 Return values
.small[
It can be useful to explicitly return()
if the routine should end early, jump out of the function and return a value.
<- function(x) {
simplefun1 if (x < 0)
return(x)
}
Functions can return only a single object (and text). But this is not a limitation because you can return a list
containing any number of objects.
<- function(x, y) {
simplefun2 <- x + y
z return(list(result = z, x = x, y = y))
}
simplefun2(1, 2)
## $result
## [1] 3
##
## $x
## [1] 1
##
## $y
## [1] 2
26.6 Challenge
Using what you have just learned on functions and control flow, create a function named bigsum
that takes two arguments a
and b
and:
- Returns \(0\) if the sum of
a
andb
is strictly less than \(50\); - Else, returns the sum of
a
andb
.
Challenge 5: Solution
Using what you have just learned on functions and control flow, create a function named bigsum
that takes two arguments a
and b
and:
- Returns
0
if the sum ofa
andb
is strictly less than50
; - Else, returns the sum of
a
andb
.
Answer 1
<- function(a, b) {
bigsum <- a + b
result if (result < 50) {
return(0)
else {
} return(result)
} }
Answer 2
<- function(a, b) {
bigsum <- a + b
result if (result < 50) {
0
else {
}
result
} }