Functions

Activity

Work on the warmup activity (handout), then we will discuss as a class

Ordering and arguments

my_power <- function(x, y){
  return(x^y)
}
my_power(x = 2, y = 3)
[1] 8
my_power(y = 3, x = 2)
[1] 8
my_power(2, 3)
[1] 8
my_power(3, 2)
[1] 9
  • If you don’t name the arguments when calling a function, R assumes you passed them in the order of the function definition

Function defaults

my_power <- function(x, y){
  return(x^y)
}

What will happen when I run the following code?

my_power(3)

Function defaults

my_power <- function(x, y){
  return(x^y)
}

What will happen when I run the following code?

my_power(3)
Error in my_power(3): argument "y" is missing, with no default

Function defaults

my_power <- function(x, y=2){
  return(x^y)
}

What will happen when I run the following code?

my_power(3)

Function defaults

my_power <- function(x, y=2){
  return(x^y)
}

What will happen when I run the following code?

my_power(3)
[1] 9

Function defaults

my_power <- function(x, y=2){
  return(x^y)
}

What will happen when I run the following code?

my_power(2, 3)

Function defaults

my_power <- function(x, y=2){
  return(x^y)
}

What will happen when I run the following code?

my_power(2, 3)
[1] 8

Function defaults

my_power <- function(x, y){
  return(x^y)
}

What will happen when I run the following code?

my_power(3)

Function defaults

my_power <- function(x, y){
  return(x^y)
}

What will happen when I run the following code?

my_power(3)
Error in my_power(3): argument "y" is missing, with no default

Function defaults

my_power <- function(x=2, y=4){
  return(x^y)
}

What will happen when I run the following code?

my_power()

Function defaults

my_power <- function(x=2, y=4){
  return(x^y)
}

What will happen when I run the following code?

my_power()
[1] 16

Function scoping

What value will the following code return?

g01 <- function(x = 10) {
  return(x)
}

g01()

Function scoping

What value will the following code return?

g01 <- function(x = 10) {
  return(x)
}

g01()
[1] 10

What if I try to look at x?

x

Function scoping

What value will the following code return?

g01 <- function(x = 10) {
  return(x)
}

g01()
[1] 10

What if I try to look at x?

x
Error: object 'x' not found
  • Variables created within functions don’t exist outside the function!

Function scoping

Variables created within functions don’t exist outside the function!

g01 <- function() {
  x <- 10
  return(x)
}

g01()
[1] 10
x
Error: object 'x' not found

Function scoping

What will the following code return?

x <- 10

g01 <- function(){
  return(x)
}

g01()

Function scoping

x <- 10

g01 <- function(){
  return(x)
}

g01()
[1] 10
x
[1] 10
  • If a variable is not defined in a function, R looks outside the function (the global environment)

Name masking

What value will the following code return?

x <- 10
g01 <- function() {
  x <- 20
  return(x)
}

g01()
x

Name masking

What value will the following code return?

x <- 10
g01 <- function() {
  x <- 20
  return(x)
}

g01()
[1] 20
x
[1] 10
  • Names defined inside a function mask names defined outside a function
  • Variables created within a function don’t exist outside

Summary

  • Variables created within a function don’t exist outside
  • If a variable is not defined in a function, R looks outside the function
  • Names defined inside a function mask names defined outside a function

Class activity

https://sta279-f25.github.io/class_activities/ca_15.html