What does R's pass-by-value mean, and how do environments give reference semantics?

Quick Overview

Asks what it means that R passes function arguments by value and why a function that edits a vector leaves the caller's object unchanged. It tests copy-on-modify and how environments give reference semantics, the basis of R6 and Reference Classes.

What does R's pass-by-value mean, and how do environments give reference semantics?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What does it mean that R passes function arguments by value, and how do environments provide reference semantics as an exception? ```hint Copying and mutability Ask what the caller sees after a function assigns into one element of its argument, and which R object type is never duplicated when it gets a second name. ``` ### What a Strong Answer Covers - What the caller's object looks like after a function modifies its argument - How R avoids paying for a full copy every time an object is passed to a function - Why environments behave differently and what the caller sees after a mutation such as `e$x <- 5` - Where R code uses reference semantics on purpose (Reference Classes and `R6`) - A short code example that contrasts the two behaviors ### Follow-up Questions - How would you confirm in a live session whether a modification made a copy of a vector? - How does `<<-` inside a closure differ from passing an environment explicitly? - `data.table`'s `:=` operator changes a table without reassignment. How does that fit with R's value semantics, and what can surprise a caller? - How do you get an independent copy of an `R6` object or an environment?

Overview: Asks what it means that R passes function arguments by value and why a function that edits a vector leaves the caller's object unchanged. It tests copy-on-modify and how environments give reference semantics, the basis of R6 and Reference Classes.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

What does it mean that R passes function arguments by value, and how do environments provide reference semantics as an exception?

What a Strong Answer Covers Guidance

  • What the caller's object looks like after a function modifies its argument
  • How R avoids paying for a full copy every time an object is passed to a function
  • Why environments behave differently and what the caller sees after a mutation such as e$x <- 5
  • Where R code uses reference semantics on purpose (Reference Classes and R6 )
  • A short code example that contrasts the two behaviors

Follow-up Questions Guidance

  • How would you confirm in a live session whether a modification made a copy of a vector?
  • How does <<- inside a closure differ from passing an environment explicitly?
  • data.table 's := operator changes a table without reassignment. How does that fit with R's value semantics, and what can surprise a caller?
  • How do you get an independent copy of an R6 object or an environment?