Supported Parameter Passing Methods:
In R, parameters are strictly passed by value, meaning values passed into the function are copied into the parameter. Consider the following code block:
change <- function(y) {
y = 7
print(y) # y has a value of 7
}
x = 6
print(x)
change(x)
print(x) # x's value is still 6
When the global variable x is passed though change(), the parameter y copies the value of x, and changes its value to 7. However, printing x after the function call shows that the original value is still there.
Most objects are also passed by value, due to them being immutable. Whatever change the function does to the parameter does not change the values of the object being passed. Consider the following code block:
modifier <- function(y) {
print(y)
y[1] = 7
y[2] = 8
y[3] = 9
print(y) # y has values 7, 8 and 9
}
x <- c(6,8,7)
print(x)
modifier(x)
print(x) # x still has values 6, 8, 7
Passing x to modifier() copies the array into y, and modifier() changes its elements. But when x is printed again after the function call, the original array is retained.
Although most objects in R are pass-by-value, R also have built-in Reference Objects, wherein any variables pointing to that particular object have the same exact copy. Consider the following code block:
library(methods)
Owl <- setRefClass(
"Owl",
fields = list(
size = "numeric",
color = "character"
),
methods = list(
initialize = function(size = 0, color = "Unknown") {
.self$size <- size
.self$color <- color
}
)
)
editOwl <- function(avian) {
# the parameter avian copies the pointer to the same object
# and edit its contents
avian$color = "Blue"
avian$size = 10
}
# make an instance of an Owl
bird <- Owl$new(size = 4, color = "Red")
print(bird$size)
print(bird$color)
editOwl(bird)
# original object is edited
print(bird$size)
print(bird$color)
When passing the bird object to editOwl(), the reference pointer pointing towards it is COPIED to the function parameter. So when the function edits the attributes of the object avian, the object bird’s attributes are also changed. In a way, this is not truly pass-by-reference due to the fact that the original object’s pointer is COPIED to the function, therefore only emulating pass-by-reference and is still upholding the strict implementation of only pass-by-value parameters.
References:
https://homepage.divms.uiowa.edu/~luke/R/references.html
https://www.rdocumentation.org/packages/methods/versions/3.6.2/topics/ReferenceClasses
https://www.geeksforgeeks.org/r-language/creating-initialize-method-for-reference-class-in-r/