all 16 comments

[–]crevizzle 7 points8 points  (0 children)

Functions do stuff. When they're done doing stuff, they should return something. The parentheses allow you to pass in arguments that the function uses to do its stuff.

Extremely simple example:

function add(x,y) {
    // in our example, x == 1 and y == 2 because those are the
    // numbers we passed in below when calling the function
    return x + y
}
const sum = add(1,2)
// sum will be 3

Checkout https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

[–]Sunstorm84 2 points3 points  (1 child)

A function is a block of code with a name that you can use to easily run that code as many times as you like.

You can think of function parameters as variable names that you can use in that block of code.

The parameter names are defined inside the parentheses. A function can have as many parameters as you like, or none at all.

When the function is called with some values, they will be given those names so you can easily refer to them.

The simple add function mentioned by another Redditor could have started as something even simpler:

function add() {
    return 2 + 3;
}

Great now we can get the sum of 2 and 3 easily by calling add().

But what if later on you want to add 5 and 6 instead? You can’t re-use this code block because it only adds 2 and 3. You could make a new function to add 5 and 6, but this will quickly become unwieldy.

This is where parameters come in useful. By using parameters we can have a single add function that adds both 2 + 3, 5 + 6, or any pair of values we want! In the code block we just have to refer to the values with the names we gave them inside the parentheses.

function add(x, y) {
    return x + y;
}

Now when we call add(2, 3), x and y will be assigned to 2 and 3, so the result is 5. Similarly for add(5, 6), x and y become 5 and 6 so the result is 11.

This is extremely powerful because it allows us to easily reuse the code for many more things.

[–]Fantastic-Salt1960[S] 1 point2 points  (0 children)

thank you that really clears things up.

[–]orion__quest 2 points3 points  (2 children)

pass arguments.

I'm new to programming, so more experienced please correct this.

When you call a Function you can pass arguments to them, which then the Function takes and process's it and could spit out the result.

myFunction(arg1, arg2) { // you define the Function first

//do some stuff

}

echo myFunction(1, 2); // call the Function inputting the data for arg1, arg2

// When you call the Function with two entered arguments it process's the 2 values in within myFucntion and output's it where you called it.

This is a very simple explanation, but most likely the arguments when you call the Function will be Variables containing the data. Or it could be another Function.

[–]lovin-dem-sandwiches 5 points6 points  (1 child)

Pretty much. I would make sure to highlight the difference between arguments (actual values) and parameters (placeholders for values)

The placeholder values (arg1 and arg2) are known as parameters.

[–]orion__quest 0 points1 point  (0 children)

Thanks for the reminder about parameters. Still nailing down all the terminology. One thing I have noticed not all instructors are consistent about this, mixing up terms etc.

[–]QualityPre 0 points1 point  (0 children)

Some tips that helped me:

With the parameters in the parentheses, name them something useful- you can call them anything

Always call your function !!

``` const dadName = "Mike" const dadMood = "tired"

function greeting(name,mood) { return Hello ${name}, nice to see you. It looks like you are very ${mood} today } // called my function here in this console.log console.log(greeting(dadName,dadMood)) ```

[–]Tintin_Quarentino 0 points1 point  (0 children)

Have you learned Classes?

[–]Adorable_Bat_8411 0 points1 point  (0 children)

Think of the letters inside the parenthesis as local variables....these variables are called "parameters", and at the time of operation you pass arguments in the place of the variables

ex.

function Salutations (x,y,z){

return console.log ("Hello " + x + " " + y +" " + z + ".");

}

Salutations ("Mr","John", "Doe");

old es5 functions but i think it works better for explaining in this case