# Solved: Javascript, defining functions. This is right isn't it?



## turbow5 (Jan 2, 2012)

It said line 1 was missing a semi-colon so I just threw it in there and it says it's right. Can someone confirm that? 

Also the problem is on line 2. Says It's missing semi-colon.

Thanks in advance.


----------



## Ent (Apr 11, 2009)

What's the code?


----------



## turbow5 (Jan 2, 2012)

Oh my god I forgot to put the picture, sorry about that.


----------



## janikPilot (Nov 26, 2010)

you're overthinking 

first of all, variables and functions are very different. you can't assign a function to a variable. that's like telling somebody to hold your running. You can't do that, since running is a process.

i believe what you are trying to do is:


```
function myFirstFunction() {
     console.log("Christopher");
}
```
Note that by convention, functions are named beginning with a lowercase letter, and all succeeding words are capitalized, as shown above.

I suggest this website if you would like more information:

http://www.w3schools.com/js/js_functions.asp


----------



## turbow5 (Jan 2, 2012)

The website your link leads to is good but it doesn't show me a example of what the website wants. It says I have to set a var, then make the function. If I'm saying that right. Here's the last section of what I had to do. You're throwing me off because you didn't set a var like the site wants. (well it started me off with "var" so I'm assuming).


----------



## Ent (Apr 11, 2009)

He's not throwing you off.
If you read what the exercise actually asks for, that code is full of mistakes. You can't use it as a guide to what the site wants, the site wants you to fix it!


----------



## turbow5 (Jan 2, 2012)

What now... It's not telling me anything wrong and I don't see anything wrong.


----------



## Ent (Apr 11, 2009)

First, define your *function*.


----------



## turbow5 (Jan 2, 2012)

Isn't that the "myfirstfunction=Christopher"?


----------



## janikPilot (Nov 26, 2010)

no, that would be a variable assignment. such as:


```
var someName = "Christopher";
```
the following is a function declaration:


```
function myFirstFunction() {
     //some code here
}
```
Variables are used to store data, such as your name or the number 16. Functions just grouped lines of code that do something, such as a hypothetical function below:


```
function displayHelloWorld() {
     var someData = "Hello World!";
     document.write(someData);
}
```
That's actually a function within a function. The function I created is called displayHelloWorld, which merely creates a variable and assigns data to it. Then the document.write function (standard in javascript) takes that data and displays it on the screen. I believe in your website you use console.log instead, as it prints to the console instead.

Remember that the equals sign (=) is merely an_ assignment operator_. It assigns data to a variable. Functions don't store data, they only execute blocks of code.

I would recommend checking out the link I sent you, as it should help you learn the basics of Javascript. Then I would try doing these exercises.

EDIT: I am not familiar with the variable assignment of a function. I did some research on it. I'm not sure it's exactly a beginner friendly topic. However, I will try to explain. (Source: http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname)

When you declare a function like this:


```
var someFunction = function() {
     //somecode
}
```
it's only understood by the computer at run-time. so if you make a call to someFunction before it is defined, it will generate an error. However, if you define a function as I mentioned above, it is understood by the computer at parse-time, which means when the computer reads it.

Visit the link for more information.


----------



## turbow5 (Jan 2, 2012)

*facepalm*

When you said var was a variable assignment, a "duh" came to my mind. Tried to fix it up and It said bad invocation. the error was "oops, try again", that..doesn't help me.

What's it saying?


----------



## janikPilot (Nov 26, 2010)

An invocation is when you call a function. It's called 'invoking' the function. You did it incorrectly  Your function name is myfirstfunction() correct? (Remember that programming style says you should capitalize every word after the first. So it should actually be myFirstFunction() ) But since your is called myfirstfunction(), you simply call it using that name, with no quotes. like this:


```
myfirstfunction();
```
This needs to be outside of the function itself. At least for your purposes. When you get more advanced, you'll learn about calling a function from inside a function (it's called recursion). But for now, make sure you call the function outside of the function.

Let me explain each line:


```
function myfirstfunction() { ... }
```
Declares the function. Basically you're telling the computer 'when I write myfirstfunction(), do the code inside the brackets.'


```
var myfirstfunction = Christopher();
```
this line is unnecessary, actually it's declaring another function (called Christopher), that does absolutely nothing since it has no brackets. Take it out. You can't define functions inside functions anyways.


```
console.log("myfirstfunction");
```
I think I get what you're trying to do. You're trying to assign a variable the text, "Christopher", and then output that to the console, correct? Well, here's how you should do that.


```
var variableName = "Your Name";
console.log(variableName);
```
The last line, 

```
"myfirstfunction"()
```
Does NOT need the quotes. It's not a string. It's a function. It should 1) be outside the curly brackets. 2) not have quotes.

I hope you're somewhat understanding it better


----------



## turbow5 (Jan 2, 2012)

It does actually. I don't know how you do the boxes with words in them. they say code and you write a example.how do you do that?

Also from the 3rd and 4rth box, Do i delete var or not?

I have to make a function. Which the "function myFirstfunction" was right?
Then I have to make it write my name. So I have to set a var. and call up the var within the function right?. I feel my brain cells dieing form this.


----------



## janikPilot (Nov 26, 2010)

You have to surround the code with [ CODE ] tags. Go to the advanced editor. Type some code then highlight it and press the hashtag (#) symbol at the top of the editor. It will put [ CODE ] tags around the code and format it like that  I put spaces in the tag so you can see it here.

delete the third line completely.

The

```
function myFirstFunction()
```
 was right. Remember that you need the parenthesis.

You got it! You set up a var and give it a value, something you want to output to the screen. then you use the var in your output.

In coding terms:

```
var variable = "some text";
console.log(variable);
```


----------



## turbow5 (Jan 2, 2012)

Line 2 missing semi colon. I'm not missing it, its right there! what? *facepalm* lol 

Also not yet, but I want to live the CS life.


----------



## janikPilot (Nov 26, 2010)

turbow5 said:


> Line 2 missing semi colon. I'm not missing it, its right there! what? *facepalm* lol
> 
> Also not yet, but I want to live the CS life.


You have an extra parenthesis on that line for no reason, where did that come from?  Errors are not always what the compiler tells you they are. It just takes its best guess at what the error is. Your actual error is the extra parenthesis.

But you are correct, just be careful when you name your variables. Don't give it that name, it's confusing. Name it something that doesn't confuse it with a function. The point is to name it something descriptive, like for instance since it contains your name, you name the variable, 'name'.

That's a website I'm trying to start, hopefully more people agree with you


----------



## turbow5 (Jan 2, 2012)

I remember how to do var. Least I think I do. 

This is how you do it right?


----------



## janikPilot (Nov 26, 2010)

Yes! You almost have it!

When you use the variable, like in a function, you must call it without quotes. Consider the following:


```
var name="Michael";
console.log(name);
```
Will display my name, Michael, to the screen. However,


```
var name="Michael";
console.log("name");
```
Will display the word name to the screen. In the second example, you are just passing the data "name" to the function, instead of the variable called name which holds the data of my name (Michael).

Note that the way you defined the function there is the correct way, but it seems your tutorial is saying it is not. I believe they want you to define it the other way, which is like 

```
var myFirstFunction = function() {

}
```
Which is very confusing to a beginner. Understand that for your purposes, 

```
function myFirstFunction() {

}
```
and


```
var myFirstFunction = function() {

}
```
Are exactly the same thing, just different ways of writing it. (They differ slightly, but since you are just starting out and the tutorial requires it, pretend they do the same thing. You can see my earlier post for their differences)


----------



## turbow5 (Jan 2, 2012)

So you are saying that I made and called a function correctly? If you are then it'll be buried in my brain.

I tried the new way you wanted me to make a function, but there's a error on line one. Says the ; but I know it isn't that.


----------



## Ent (Apr 11, 2009)

It is a problem with the semicolon. Remember that the semicolon is to Javascript what the Full stop or period is to us. 

In essence you've written
And now I'm going to teach you how to.

Remove that semicolon.


----------



## turbow5 (Jan 2, 2012)

Thank you for telling me that actually. The website never told me that the ; was a period to us.

So I took that out and this is what I'm left with.

var myFirstfunction=function (){
console.log ("christopher");
christopher ();}

Line 3 is my problem now..hmm...missing semi colon. I don't think that could be it...I'm stairing at this trying to figure it out...


----------



## Ent (Apr 11, 2009)

Line 3 tells it to execute the function called cristopher. What function would that be? 

Also, it's inside the function you're working with, which means that once you do fix it the function will end up calling itself every time it runs. That has some rather unfortunate consequences, as the program can never complete (as it can't escape from that function) and will end up crashing.


----------



## turbow5 (Jan 2, 2012)

Ok so line 3 execute the function Christopher. But the var makes myFirstfunction=function. Then from the prevoius section I was told to tell the console..which is console.log..which "christopher" is what the function is suppose to display right? so........... I call it up by myFirstfunction..right? But If I do that then I'm missing semi colon.

No wonder you gotta go to college for this stuff. It gets hard fast.


----------



## janikPilot (Nov 26, 2010)

turbow5 said:


> Ok so line 3 execute the function Christopher. But the var makes myFirstfunction=function. Then from the prevoius section I was told to tell the console..which is console.log..which "christopher" is what the function is suppose to display right? so........... I call it up by myFirstfunction..right? But If I do that then I'm missing semi colon.
> 
> No wonder you gotta go to college for this stuff. It gets hard fast.


Oh it's not that bad 

No, you're doing it right. If the function that prints "Christopher" to the console is called myFirstFunction(), then that's the name you use to call it OUTSIDE of the function itself. You were calling it from the inside.

So close the function (end it with a } bracket) and on the next line type myFirstFunction(); Remember to place the semicolon at the end of the statement.


----------



## turbow5 (Jan 2, 2012)

School started and this will blow my brains out .

Is a function a "must-know" thing? from this exercise it seems I just set a variable and called it up.


----------



## Ent (Apr 11, 2009)

turbow5 said:


> Is a function a "must-know" thing?


Yes. I would go as far as to say you cannot use Javascript without a solid understanding of functions.



> from this exercise it seems I just set a variable and called it up.


There is a fundamental difference between a function and a variable. 
A variable refers to a piece of information, 96, "Christopher" or FALSE for example.
A function contains instructions for the computer. 
Thinking about functions as just another variable is like thinking of a recipe as an ingredient.


----------



## janikPilot (Nov 26, 2010)

The thing that is really confusing him though is this tutorial, since it introduced the var declaration of a function WAY too early. The first way you should learn how to program a function is simply "function name()", not "var name = function()" since that is extremely confusing, even for me.

turbow5, I don't know what else we can tell you. We've explained everything.


----------



## turbow5 (Jan 2, 2012)

Yeah I had a feeling you two did. I'm just going to take a break from it. Come back later, and see if I can reconfigure it in my skull.

Also I'm youtube'ing this till I get it. I really do appiciete you two for helping though. 

Janikpilot did you say from post #17 that I did the function right in a way?


----------



## janikPilot (Nov 26, 2010)

yes, you did the function declaration totally correct. Only problem with your code was you used quotes around the variable name.

No problem, post again or just PM me if you need help later


----------



## turbow5 (Jan 2, 2012)

I will remember that, won't be for a while though.


----------



## ckphilli (Apr 29, 2006)

janikPilot said:


> The thing that is really confusing him though is this tutorial, since it introduced the var declaration of a function WAY too early. The first way you should learn how to program a function is simply "function name()", not "var name = function()" since that is extremely confusing, even for me.
> 
> turbow5, I don't know what else we can tell you. We've explained everything.


It's really a great tutorial. You have to understand Turbo has been doing Codeacademy for a while now (think this is the second 'set'), so the introduction really isn't that early.

The thing about Codeacademy is that you need to read and understand the directions and the output.

Turbo, you've got to slow down buddy, no offense intended.


----------

