First of all, I am initializing an empty vector and I want to populate it whenever I call the function. I want the element added to be added to the empty vector and so on but, when I call the function nothing happens so I not sure what I am doing wrong. Any help is appreciated.
empty_vec<-c()
func<-function(num){
for (numbers in num) {
i<-sqrt(numbers)
empty_vec<- c(empty_vec,i)
}
}
func(4) # When calling the func,4 isn't getting added to the empty_vec.
<-will not overwrite the value of theempty_vecoutside offunc, it will make a "copy" of the data within the function that masks it, and when the function exits, that updated copy will cease to exist. (2) There are functions for doing that (see<<-andassign), but they should not be used regularly, and suggest a poor design. I discourage them.