I wonder whether a method in a functional programming language should receive all variables from the argument list, or whether it is ok to use variables from the outer scope?

But let me explain the context firts: The enviroment where I'm working is focused on programming (or better: scripting) Scala in the Scala-REPL / Scala-Notebook. So all methods we define are just used within the REPL, so there is no "outer" scope. The methods we define are never used from "outside" as you would do in a normal OO environment. So by methods, we actually mean functions...

Given this premise, I always try to define my functions (more precisely: methods) to be self-contained, i.e. that no variable is accessed from the outer scope. This has the advantage that I can re-arange my code, or even copy-paste my method to another script and re-use it. The disadvantage is that the method's argument-List is blown up and may become very long. In this case, I normally define dedicated data structures (e.g. for constants) and pass them in as 1 variable.

So my question is: Should I always pass all variables excplicitly or (under what circumstances) is it fine to access the outer scope?

1 Answer 1


It's fine to use constants (including references to other functions) from the outer scope. If you copy your function to another file and those symbols aren't defined, you'll get a compiler error, the same as if you forgot to pass them as an argument. There is very little difference semantically. The problem is if those values from the outer scope can change. Then they should be passed in as arguments.

What concerns me is you are saying this is making your argument lists very long. This means most likely you would be better off splitting your functions into smaller, more cohesive, pieces.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.