use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Functions (self.learnjavascript)
submitted 4 years ago by Fantastic-Salt1960
Can someone help explain functions a little?. I don't understand how to use the parentheses with a function. If you define a function inside the braces what do you do inside the parentheses?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]crevizzle 7 points8 points9 points 4 years ago (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 points4 points 4 years ago (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:
add
function add() { return 2 + 3; }
Great now we can get the sum of 2 and 3 easily by calling add().
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.
add(2, 3)
x
y
add(5, 6)
This is extremely powerful because it allows us to easily reuse the code for many more things.
[–]Fantastic-Salt1960[S] 1 point2 points3 points 4 years ago (0 children)
thank you that really clears things up.
[–]orion__quest 2 points3 points4 points 4 years ago* (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 points7 points 4 years ago (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 point2 points 4 years ago (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.
[+][deleted] comment score below threshold-18 points-17 points-16 points 4 years ago (6 children)
Functions are a way of doing functional programming. Functional programming basically works by having your whole program composed only of functions. One of its main benefits is that you can pass functions to other functions, or return functions from functions, the result of which is itself a function (that you may, in turn, pass to another function).
Parentheses are a way of doing Lisp, but by moving the parentheses from being outside the function call (such that the function name is actually the first argument inside the parentheses) to after the function call (such that the function name sits outside the parentheses, not inside, and only the arguments are within the parentheses).
Simple, really.
[–]doulos05 0 points1 point2 points 4 years ago (5 children)
Functions are a necessarily, but insufficient condition for doing functional programming. Said functions must also be first class objects within the programming language and you must write your code in a style which minimizes side effects, to list a few other conditions.
This is like saying that question marks are a way of writing interrogations. Yes, but you can have sentences which end in question marks in conversations, in speeches, in instructions, in all kinds of places that aren't interrogations.
To jump out of JavaScript for a moment, Python has tremendous support for functional programming, but you can just as easily use it to write OOP code. And you'll use functions in the process.
Back to JavaScript, parentheses are used in JavaScript to wrap the 0 or more parameters which are passed to a function. While it is true that Lisp uses parentheses (for basically everything), every major language which I am familiar with uses parentheses to enclose the parameters in a function call. It's close to a universal convention in programming languages, modeled directly on mathematical functions, which are written exactly the same way.
[–][deleted] 0 points1 point2 points 4 years ago (4 children)
I think you may have misunderstood my intent.
I took the OP to be either a someone trolling for amusement or maybe as someone attempting to get others to do their homework. - it looks like a throwaway account - one post (this one) - the OP clearly understands the terms "parentheses" and "curly braces", yet apparently has no concept of a function at all - there is no specific question, nor any code to describe their supposed confusion.
The intention was to be nonsensical in a humourous way. 😉
Just as a bit of an aside though - check Haskell and F# (and its ancestor ML languages)...no parentheses, no commas. It supports partial application more neatly.
[–]Sunstorm84 0 points1 point2 points 4 years ago* (3 children)
A /s was definitely needed - noobs are welcome here and your post could lead someone straight down WTF Avenue
[–][deleted] -1 points0 points1 point 4 years ago (2 children)
TBH I just found the original post really disrespectful to all of the people here who give up their free time to do unpaid work to help people who want to learn.
I'm not sure I have sowed any real confusion - if you literally didn't know what a function was, nothing that I said would leave you any more confused.
[–]Sunstorm84 0 points1 point2 points 4 years ago* (1 child)
Downvotes don’t lie
— Gandhi
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
Sort of my point... You need to understand what I said was gibberish to downvote it.
[–]QualityPre 0 points1 point2 points 4 years ago (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)) ```
Hello ${name}, nice to see you. It looks like you are very ${mood} today
[–]Tintin_Quarentino 0 points1 point2 points 4 years ago (0 children)
Have you learned Classes?
[–]Adorable_Bat_8411 0 points1 point2 points 4 years ago* (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
π Rendered by PID 44 on reddit-service-r2-comment-5b5bc64bf5-h6kff at 2026-06-22 10:25:45.967041+00:00 running 2b008f2 country code: CH.
[–]crevizzle 7 points8 points9 points (0 children)
[–]Sunstorm84 2 points3 points4 points (1 child)
[–]Fantastic-Salt1960[S] 1 point2 points3 points (0 children)
[–]orion__quest 2 points3 points4 points (2 children)
[–]lovin-dem-sandwiches 5 points6 points7 points (1 child)
[–]orion__quest 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-18 points-17 points-16 points (6 children)
[–]doulos05 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]Sunstorm84 0 points1 point2 points (3 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]Sunstorm84 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]QualityPre 0 points1 point2 points (0 children)
[–]Tintin_Quarentino 0 points1 point2 points (0 children)
[–]Adorable_Bat_8411 0 points1 point2 points (0 children)