Scope en prioriteit

Introductie tot functies schrijven in R

Richie Cotton

Data Evangelist at DataCamp

Variabelen buiten functies benaderen

x_times_y <- function(x) {
  x * y
}
x_times_y(10)
Error in x_times_y(10) : 
  object 'y' not found
x_times_y <- function(x) {
  x * y
}
y <- 4
x_times_y(10)
40
Introductie tot functies schrijven in R

Functievariabelen van buitenaf benaderen

x_times_y <- function(x) {
  x * y
}
y <- 4
x_times_y(10)
print(x)
Error in print(x) : object 'x' not found
Introductie tot functies schrijven in R

Wat is beter? Binnen of buiten?

x_times_y <- function(x) {
  y <- 6
  x * y
}
y <- 4
x_times_y(10)
60
Introductie tot functies schrijven in R

Meegegeven vs. gedefinieerd

x_times_y <- function(x) {
  x <- 9
  y <- 6
  x * y
}
y <- 4
x_times_y(10)
54
Introductie tot functies schrijven in R

Laten we oefenen!

Introductie tot functies schrijven in R

Preparing Video For Download...