Reputation:
This is perhaps a dumb question, but I am new to Javascript and desperate for help.
If the Javascript engine will look for global variables outside of a function, then what is the point of passing parameters to it? What do you gain?
I understand that global variables are generally frowned upon, but I still don't understand the purpose of passing variables. Does it have something to do with data encapsulation?
Upvotes: 1
Views: 244
Reputation: 15582
There's a few magic words that are used by programmers to describe different kinds of functions. Here's a few:
You can look some of them up if you want a headache. The point is that Computer science and engineering progress has always been about reducing complexity. We have spent quite a lot of time thinking about the best way to write a function to achieve that goal. Hopefully, you can stuff tiny bits of your program into your head at a time, and understand those bits, without having to also understand the overall functioning of the entire program simultaneously, or the detailed implementation of the insides of all the other functions. A function that uses global variables can't do that very well because:
On the other hand, if you explicitly pass in all the data a function needs to operate, and explicitly return all the results:
And other advantages.
Upvotes: 4
Reputation: 681
Functions are reusable components of your code, that executes a particular snippet on the provided variable exhibiting varying behavior. Encapsulation comes from being Object Oriented. Functions are more for giving structure to your program. Also, you shouldn't undermine the execution time of a method, if the variable it access exists in the context rather than being global.
Upvotes: 1
Reputation: 1706
Is this really a javascript question? I haven't encountered a language that doesn't have some form of global variables yet.
Upvotes: 0
Reputation: 83699
if you were only going to use global variables that the functions worked on then you'd always have to know the inner workings of the functions and what your global variable names had to be for them to work.
also, something like Math.abs(n) would be hard to call twice in one line if using global variables.
Upvotes: 1
Reputation: 16242
That said, variables global to a namesapce can be useful especially if you are using something like jquery selectors and you want to cache for performance sake.
Upvotes: 0
Reputation: 19030
One of the main benefits is that it keeps all the information the function needs, nearby. It becomes possible to look at just the function itself and understand what its input is, what it does, and what its output will be. If you are using global variables instead of passing arguments to the function, you’ll have to look all over your code to locate the data on which the function operates.
That’s just one benefit, of many, but an easy one to understand.
Upvotes: 0
Reputation: 33197
If you don't need parameters to be passed to functions, then, you really don't need functions.
Functions are usually (and should be) used to provide code re-use -- use the same function on different variables. If a function accesses global variables, then every time I use it it will perform the same action. If I pass parameters, I can make it perform a different action (based on those different parameters) every time I use it.
Upvotes: 0