Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBret Lynn
7,037 PointsReview Creating Reusable Code With Functions
One of two quiz questions completely blocking me in the final quiz of the JavaScript Basics course:
Given this function, write the code that will call it:
function warning() { alert('Warning, warning, warning); }
My understanding is that there are multiple ways to call a function....such as using console.log, document.write, alert, and others I'm sure. I've tried all I can think of, and still I'm wrong. So, what is the question asking me to do?
Stabbing myself in the eyes.
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! No, there is a universal way to call a function. You give the name of the function, a set of parentheses, any arguments you are passing to the function, and then a semicolon.
Keep in mind that all methods are functions, but not all functions are methods. Methods are functions that are tied to a class
. In your examples above, write
is a function/method on the document
object, log
is the method on the console
object, and alert
is a method on the Window
object. So the functions in those are , write, log, and alert.
Here is a typical call to a function:
functionName(arguments);
And that is generally all that is required. So for your answer, the function name is warning
and it needs no information passed into it, thus making the answer to the question: warning()
Note: the quiz was nice enough to include the semicolon on the end itself.
Hope this clarifies things!
Bret Lynn
7,037 PointsBret Lynn
7,037 PointsYes, that worked, thanks for your help. The terminology is not clicking for me, though. Do you recommend any books on JavaScript? I've heard that Eloquent JavaScript is good.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherTo be honest, most of my JavaScript knowledge comes right here from Treehouse. I have not had time to read a lot of books, though I've read plenty of documentation.
Earlier today, I posted another answer to someone about functions and why we even need them. It might be worth reading. Take a look here
I can say a few words about the terminology. And I'll use a very simple example.
Ok so first we define our function. When we do this, we give the function a name (generally). You will get to anonymous functions later, but that's a bit down the line. The things in the parentheses inside the definition are called parameters. We also say what the function will do. The lines that say what the function will do inside the curly braces are called the body of the function.
Now here, it gets a little odd with semantics. Some programmers use the terms parameters and arguments interchangeably, but semantically speaking, that's not technically correct. Parameters accept the values coming in. Arguments are the values being sent. Arguments are the things in the parentheses at the call of the function.
I added a call to
addTwo
which has two arguments of 3 and 9. A call to the function is a request to the function to execute. The 3 will be assigned to the local variablenum1
and the 9 will be assigned to the local variablenum2
. We add those two together and then return the value.In the first instance of the call to the function, it doesn't really do anything. It adds those two numbers together, sends them back and then nothing else happens because we've not assigned a result to a variable or displayed it. But in the second call to the function, we pass in the arguments 7 and 20. This time 27 will be returned to the call site. The call site is located within an alert function, so you will receive an alert with 27 printed out.
Hope this helps!