#5 Seriously unhappy with these function lessons


#1

Can someone help me again please. i'm sorry to keep asking but i dont understand these function lessons one bit. why doesn't this work???

can someone explain what i'm trying to acheive on this lesson. im supposed to be rewriting this to debug it but it looks like there isnt even a function in the code i'm given.

given this:
greeting var func{name}(console.log(name)))}

rewritten it this:
var greeting = function ("function 1")
{(console.log(greeting);}


#2

try going back through the lessons untill you understand

try

var greeting = function(name)
{
console.log(name);
};
greeting("hello")


#3

it doesn't help at all. i can complete the lesson no problem but there's a vital piece of information missing. Why do you call the function using the name of var and then in the console.log use the name in brackets? this is what i don't understand. why does it have two names?


#4

try this:

var greeting = function(name)
{
console.log(name)
};
greeting("hello");


#5

Hi @jamesm049,

Having a scour on the internet becomes really helpful, I mean no disrespect here, simply when I went through CC lesson I found many a case that some other explanation delivered a better understanding to me than the one given in the lesson, kind of like having a fellow classmate explain what the tutor just tried to say but left me confused. With that said then.

Lets look at a function.

var greeting = function(name)
{ 
  console.log(name)
};
greeting("hello");

There are several parts to this, just like we declare variables and give them a name, we do the same for functions, there are two ways they can be written out, but we will focus on the example we have.

var greeting = function(name)

All we do here is declare a variable, give it a name and set/assign a value to that variable: We declare a var called greeting, and assign to it the value of a function.

Now a function performs some action, and that said action is accomplished by the code in the curly braces, so we add those in next. { }

var greeting = function(name)  //we declared a variable greeting and assigned to it a function, we could have assigned an int or string etc.
{  //everything inside these curly braces is what the function does, be it sum something up or print something out
  console.log(name) //here is an example of the functions action, it will print something to console.log()
};

finally, in order to use a function, just like you would use a variable of any kind you have to call/invoke it by name, and this is where this comes in.

greeting("hello");

So altogether we have the following.

var greeting = function(name) //declare a var and give it a name, assign to it a function
{ 
console.log(name) //make our function perform this action when we use it
};
greeting("hello"); //call our function by name

Now on to the final piece of the puzzle. Functions can take some type of input, this is optional not all functions want input: These are called parameters when we refer to them by name and an argument when we pass in a value, don't get too caught up on this. =)

So what we know is that a function can take some input so that it can perform something, to accomplish this we basically declare a type of variable in the parens of the function, in this case we use 'name'

function(name)

Now all this is is a placeholder that we can then use inside our function to do some action. But much like our variables we need to refer to them by name so that our code knows what we are talking about. With me so far? =)
This is partially to do something with something called scope, but that's for another day unless you are already aware of it.
Back to finishing this off. So outside when we call the function, we put the variable we want to perform an action on in the parens, EG: "hello", our function then receives this and uses the placeholder name we originally set EG: 'name' and substitutes it inside the function to work with. So all you need to think of is how name actually == "hello" but our function uses name as a placeholder to work with.

Lets re-cap.
A function is a piece of code that performs an action. It needs to be declared and have a name so that we can call it and our code will know what we are talking about.
Whatever is inside a functions curly braces { } is what action our function will perform, and to call out function we have to use it's name including the parens, so our code knows we called a function.

var greeting = function(name)
{ 
  console.log(name)
};
greeting("hello");

Finally, functions can take inputs to perform an action on, these are often called parameters and arguments. When we call a function we fill out the input we want to perform an action with by entering that variable name between the parens, and when our function works its magic it will use the placeholder name inside it's code just as a representation of the value it should work on.


3. (Objects II) Literally Speaking method confusion