When to use parameters?

Hello,

I noticed recently that I don’t use any parameters in my functions. Apparently, you can have the same outcome without them. Is that really so? Although I get the feeling that we should use the parameters as frequently as we can… or not? What are the advantages of scripts that have functions with parameters over those without parameters? What are best practices when using parameters?
I just wondered at the fact that my functions so far didn’t take any parameters, yet the applications were running correctly.

Parameters are necessary in proper programming. If you function did not have parameters and it worked, it is probably because you declared them elsewhere:

var x = 1
var y = 1

function add(x, y)
  return x + y

will work anyway without parameters:

var x = 1
var y = 1

function add()
  return x + y

because you have them declared above. Parameters are necessary, especially when you are using third-party libraries and running complex code. Most properly written code will not have parameters as “apparently optional”. It would be annoying and illogical to write code like this:

var addX
var addY

var minusX
var minusY

function addX()
  return addX + addY


function  minusX()
  return minusX + minusY

Also, parameters are used in creating objects, arrays, and so much more. Yes parameters are necessary… use them.

2 Likes

I’m guessing that you are using global variables. Here is a stack overflow on why global variables are bad practice.


var one = 1; // global variable
var two = 2;  // global variable

function addOnePlusTwo(){
  return one + two;
}
var onePlusTwo = addOnePlusTwo();
2 Likes