What is the point of parameters?


#1

Hi, I don't know if this is the right place to ask this, but I don't understand what parameters in functions are for. Sometimes it says they're not necessary and other times you need them? I have no idea. So if someone could explain it for me that would be great thank you.


#2

@maryr0831 see parameters are the input we give to function to run it.

It depends how much parameters u have given in function during function declaraing..

Var Name = function ()
{
};

Name (yourname);

here yourname is parameter, it depends how much parameters u define declaraing function that u r gonna use


#3

Well let's take two different functions and see their perks.

function sayHello(){
    console.log("Hello");
}

This function is very basic, everything inside of the function will stay the same no matter what, and you can run this function as many times as you want! YAY!

So if you have a function with 50 lines of code, and you just want it to run all of those lines of code then you can make a function with no parameters like the one above! YAY, YIPPIE. COOL! LoLMaN what's your point?

Well let's look at another function:

function sayHelloToMe(myName){
   console.log("Hello " + myName + ". What's up?");
}

Now you can call that function and give it a parameter, any parameter you want. Or you can even have the user type something in! But for now let's just have the computer say "Hello Alex. What's up?" by adding one line of code.

function sayHelloToMe(myName){
   console.log("Hello " + myName + ". What's up?");
}

sayHelloToMe("Alex"); /*Now instead of typing "console.log("Hello Alex. What's up?");
we can just say this line! 

But we want to say Hi to Bucky too! */
sayHelloToMe("Bucky");

Again this must seem pretty silly. "Why don't I just type in the console.log() statement instead?" Well what if you have 50 lines of code in that function? You wouldn't want to re-write all of that code over and over again would you? I think NOT!

I hope you find this useful!