Q:

Why does this work as a for loop but not within a function?

I'm trying to write a function that identifies if a number within a numerical vector is odd or even. The numerical vector has a length of 1000.
I know that the for loop works fine, and I just wanted to generalize it in the form of a function that takes a vector of any length
out<-vector()
f3<- function(arg){
  for(i in 1:length(arg)){
    if((arg[i]%%2==0)==TRUE){
      out[i]<-1
    }else{out[i]<-0
    }
  }
}

When run within a function, however, it just returns a NULL. Why is that, or what do I need to do to generalize the function work with any numerical vector?

A:

As already mentioned by PKumar in the comments: Your function doesn't return anything, which means, the vector out exists only in the environment of your function.
To change this you can add return(out) to the end of your function. And you should also start your function with creating out before the loop. So your function would look like outlined below.
Note, that I assume you want to pass a vector of a certain length to your function, and get as a result a vector of the same length which contains 1 for even numbers and 0 for odd numbers. f3(c(1,1,2)) would return 0 0 1.
f3 <- function(arg){

  out <- vector(length = length(arg), mode = "integer")

  for(i in 1:length(arg)){

    if((arg[i]%%2==0)==TRUE){ # note that arg[i]%%2==0 will suffice
      out[i]<-1
    } else {out[i]<-0
    }
  }

  return(out) # calling out without return is enough and more inline with the tidyverse style guide
}

However, as also pointed out by sebastiann in the comments, some_vector %% 2 yields almost the same result. The difference is, that odd numbers yield 1 and even numbers 0. You can also put this into a function and subtract 1 from arg to reverse 0 and 1 :
f3 <- function(arg){
  (arg-1) %% 2
}

