Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Review Creating Reusable Code with Functions

Bret Lynn
Bret Lynn
7,037 Points

Review 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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. :smiley:

Hope this clarifies things! :sparkles:

Bret Lynn
Bret Lynn
7,037 Points

Yes, 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

To 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.

// This is the function definition
function addTwo(num1, num2) {
    return num1 + num2;
}

// This is the function call
addTwo(3, 9);  

// This is an alert with the call to the function inside it
alert(addTwo(7, 20));

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 variable num1 and the 9 will be assigned to the local variable num2. 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! :sparkles: