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.)
funwithout providinga. So the function does not use the globalabut it's first argument to calculatea + b. The fact that you call it using the globalais 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