Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Use Function Parameters Vs. Global Variables?

Yes, I know that this is probably a very stupid question, but this has been bugging me for a while.

Ok, so I have been learning JavaScript for a while now and have understood everything perfectly. . .except for function "parameters" (I believe they are called).

I was taught that they work like so:

function test(number) {
  document.write(number);
  };

test(1);
test(12);

This makes perfect sense to me. However, recently, I've come across some that were a little different.

var counterDays = document.getElementById('days');
var counterHours = document.getElementById('hours');
var counterMinutes = document.getElementById('minutes');
var counterSeconds = document.getElementById('seconds');


var date = new Date('December 28, 2016 00:00:00');

function updateTimer(date) {
    
    var time = date - new Date();
 
    return {
        'days': Math.floor(time / (1000 * 60 * 60 * 24)),
        'hours': Math.floor((time/(1000 * 60 * 60)) % 24),
        'minutes': Math.floor((time / 1000 / 60) % 60),
        'seconds': Math.floor((time / 1000) % 60),
        'total': time
    };
};

function startTimer(counterDays, counterHours, counterMinutes, counterSeconds, date) {
    
    var timerInterval = setInterval(function() {
        var timer = updateTimer(date);
        
        //Changes the text of the 'counter'
        counterDays.innerHTML = timer.days;
        counterHours.innerHTML = timer.hours;
        counterMinutes.innerHTML = timer.minutes;
        counterSeconds.innerHTML = timer.seconds;
      
      window.onload = function() {
    
    startTimer(counterDays, counterHours, counterMinutes, counterSeconds, date);
        };
 <span id="days">&nbsp;</span>
 <span id="hours">&nbsp;</span>
 <span id="minutes">&nbsp;</span>
 <span id="seconds">&nbsp;</span>

What I seriously do not understand is why the updateTimer always needs date within the parentheses, when the variable date is an already existing variable within the global scope. Same with startTimer. I don't understand why I need to pass that in. Why not just access the variable within the function, as they do have a global scope, and be done with it. Instead I need to pass in the variable as a parameter, for the function to work.

I've tried and tried, but the only way to make the function work is by passing in the variables. Why is that???

As I am still learning, I've searched the internet for more information on functions and their parameters, but all show me something similar to my first example. I know this is all probably just going over my head, but for the life of me, I just do not understand.

Note: I am still learning, so sorry if this whole question is plain stupid.

Also, the the code for the JS that I am having a problem with won't actually run. This is due to me not wanting to put in all of my code, but rather just the code I am having trouble with.

like image 413
BZCC Avatar asked Dec 24 '16 03:12

BZCC


People also ask

Why are parameters better than global variables?

Parameter passing - allows the values of local variables within the main program to be passed to sub-programs without the need to use global variables. The value of these variables (or a copy of the value of these variables) is passed as a parameter to and from sub-programs as necessary.

What is the benefit to passing parameters in and out of function instead of using global variables?

Parameter passing is usually more memory efficient because the memory allocated to the parameters is automatically de-allocated when the function returns to the main program. It is then available for the next function call to use.

What are the benefits of using functions with parameters?

The main benefits of using parameters are: Worksheet data can be analyzed using dynamic user input. Workbooks can be targeted easily to specific groups of users.

What are two reasons why you should not use global variables?

Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.


1 Answers

Instead I need to pass in the variable as a parameter, for the function to work.

You dont need to define your functions with parameters. You can invoke them leveraging higher scope variables

https://developer.mozilla.org/en-US/docs/Glossary/Scope

This:

var x = 'baz';
function foo(x) {
  return x;
}
foo(x);

will do the same thing as:

var x = 'baz';
function foo() {
  return x;
}
foo();

Writing functions that take parameters as input helps keep your code modular and side effect free among many other benefits...

1.) The second example will always throw an error if x is not accessible at a higher scope

2.) If another function mutated the value of x it would affect the output of the second example and would lead to unexpected and potentially hard to debug behavior in your application. Whereas I can always be sure of the output of the first example

3.) It is much easier to read through and maintain code that is written in the style of the first example

like image 160
Maxwelll Avatar answered Oct 18 '22 21:10

Maxwelll