1

I have one question about using global variable in R. I write two examples

First version:

a <- 1
fun <- function(b){
    return(a+b)
}
fun(b)

Second version:

a <- 1 
fun <- function(a,b){
    return(a+b)
}
fun(a,b)

I want to know which version is correct or recommand.

3
  • 3
    What do you mean which version is correct? They both work. Define "correct". Commented Apr 19, 2017 at 14:42
  • 3
    In the second version, you cannot call fun without providing a. So the function does not use the global a but it's first argument to calculate a + b. The fact that you call it using the global a is another matter. In general, try to avoid global variables and always pass variables in function arguments like your second version does. It's not a matter of 'correctness' but rather good practice, which helps avoid bugs, makes your code more readable and easier to change, etc. Hadley's book has a good section on functions and scoping adv-r.had.co.nz/Functions.html Commented Apr 19, 2017 at 14:45
  • ok, got it. THX! Commented Apr 19, 2017 at 15:08

1 Answer 1

2

Relying on global state inside functions is frowned upon for various reasons (which can be roughly grouped under the caption encapsulation. So the second version would be superior in most scenarios.

However, the situation changes once the variable is defined inside a non-global environment. In that case, you’ve encapsulated your state into something that’s put away neatly. This is sometimes useful, because it allows you to create functions based on some input.

The classical example is something like this:

adder = function (value_to_add) {
    function (x) {
        x + value_to_add
    }
}

This may look obscure but it’s simply a function that returns another function: you can use it to create functions. Here, for example, we create a function that takes one argument and adds the value 5 to it:

add5 = adder(5)

And here’s one that adds π to its argument:

add_pi = adder(pi)

Both of these are normal functions:

> add5(10)
[1] 15
> add_pi(10)
[1] 13.14159

Both of these functions, add5 and add_pi, access a variable, value_to_add, that’s outside of the function itself, in a separate environment. And it’s important to realise that these are different environments from each other: add5’s value_to_add is a different value, in a different environment, from add_pi’s value_to_add:

> environment(add5)$value_to_add
[1] 5
> environment(add_pi)$value_to_add
[1] 3.141593

(environment(f) allows you to inspect the environment to which a function f belongs. $ is used to access names inside that environment.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.