user154759
user154759

Reputation:

Javascript Function Scope

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

Answers (7)

Breton
Breton

Reputation: 15582

There's a few magic words that are used by programmers to describe different kinds of functions. Here's a few:

  • Re-entrant
  • ThreadSafe
  • Referentially Transparent
  • Idempotent
  • Pure
  • Side-Effects

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:

  1. You can't guarantee that the global variables exist
  2. You can't guarantee that the global variables are what you think they are
  3. You can't guarantee that other parts of the program haven't modified those variables in a way you didn't expect.
  4. You can't easily generalise to use the function multiple times on multiple sets of variables.
  5. You can't easily verify that the function works as advertised without first setting up the function's external environment and its dependencies.
  6. If the global variables have changed in a way you didn't expect, it's really hard to track down which part of the program is the culprit. It could be any of 500 different functions that write to that variable!

On the other hand, if you explicitly pass in all the data a function needs to operate, and explicitly return all the results:

  • If something goes wrong with any of those variables, it's easy to find the source of the problem
  • It's easier to add code to verify the "domain" of your inputs. Is it really a string? Is it over a certain length, is it under a certain length? Is it a positive number? is it whole, or fractional? All these assumptions that your code needs to operate correctly can be explicit at the start of the function, instead of just crossing your fingers and hoping nothing goes wrong.
  • It's easier to guess what a particular function will actually do, if its output depends only on its input.
  • a function's parameters are not dependant on the naming of any external variables.

And other advantages.

Upvotes: 4

a6hi5h3k
a6hi5h3k

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

camomileCase
camomileCase

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

John Boker
John Boker

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

Scott McKenzie
Scott McKenzie

Reputation: 16242

  • Global variables (in any language) can sometimes become stale. If there is a chance of this it is good to declare, initialise and use them locally. You have to be able to trust what you are using.
  • Similarly, if something/someone can update your global variables then you have to be able to trust the outcome of what will happen whenyou use them.
  • Global variables are not always needed by everything so why keep them hanging around?

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

Nate
Nate

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

Nicole
Nicole

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

Related Questions