Exercises
(1) Conceptually describe the function we described in the above section, in other words
- what would be the input(s) to the function?
- what would the function return as output?
- what are the intermediate results that are created by the function?
(2) Here's a basic user-defined R function:
p <- 5
do.something <- function(n, d) {
m <- n + p
return(m/d)
}
- What is the name of the function?
- What are the function's arguments?
- What are the local and global variables?
- What is the "body" of the function?
- What does the function return?
- What happens to
m
andd
(local variables) once the function finishes running?
Try to predict what the function returns in the two cases here:
do.something(10, 3)
p <- 8
do.something(10, 3)
(3) Change the above function so that p
is always 5 for the function. There is more than one way to do this.