Memory allocation for objects in R

You are not comparing like with like. Your function prints "Hello, world!" but returns nothing. Therefore when you run object.size(f(1)), you are essentially running object.size(NULL), which is 0 (the size of nothing).

If you change your function so that it returns a string, it would be equivalent to Method 2 (assigning a value to a string):

f <- function(num) {
    for (i in seq_len(num)) {
        return("Hello, world!\n")
    }
}

object.size(f(1)) # 120 bytes
object.size(f(1)) == object.size("Hello, world!") # TRUE

In either case you are measuring the size of the string. However, simply allocating the string in this case is going to use less RAM, as the function itself takes up space in memory:

object.size(f) # 16088 bytes

Even an empty function in R will take up about 4k of RAM:

g  <- function() {
    return(TRUE)
}
object.size(g) # 4384 bytes

Having said this, given that in R you are generally working with data held in RAM, the amount of overhead storing functions is usually negligible by comparison. Most of the time in R you will want to optimise for clarity of your code (i.e. use functions), rather than small amounts of RAM. There are exceptions though (e.g. calling a function millions of times in a loop).