Solutions

Here's the solution to part (3). As we mentioned, there's more than one way of doing this. One way is to turn p into an argument for the function do.something, with the default value set to 5.

do.something <- function(n, d, p = 5) {
  m <- n + p
  return(m/d)
}

Another approach is to make p a local variable, instead of a global variable. When the function runs, regardless of what p is assigned to globally, p will always be assigned the value 5 in the function's environment.

do.something <- function(n, d) {
  p <- 5
  m <- n + p
  return(m/d)
}

p <- 8
do.something(10, 3)
[1] 5

results matching ""

    No results matching ""