How does a function work 4/13
I do not know what I’m doing wrong, can somebody help please!
var foodDemand = function(food)
foodDemand = (“Pizza”); <-- move this line to the end. + remove =
{
console.log(“I want to eat”+” “+ food);
}; <-- remove semicolon
<-- here
Edited by a moderator
Answer 53bddcf180ff33df72000129
Hi Daniel, let’s talk functions.
The name function comes from functionality. A function is like a closed box. Inside of the box there is a mechanism that performs some action. This action is programmed by the creator of the function. If you are the programmer you create the functionality inside of it.
So in a way, functions are like memory blocks used to save a mini program inside them.
A function has an input we call parameters, the body of the function does the magic stuff, and then it outputs a result:
The function on lesson 4/13 is called foodDemand.
It has one input via a parameter called food
and it outputs : I want to eat some inputtedFood
Translated to JavaScript it goes like this:
var foodDemand = function(food)
{
console.log("I want to eat" + " " + food);
};
Then we can call the function and feed it any kind of food, like for example “Pizza”.
“Pizza” will replace the parameter food and the output will be:
I want to eat some Pizza
To call the function with Pizza we do it this way:
foodDemand("Pizza");
Hope it helps :)
P.S. Here’s another way of writing the same function. It is just a different style but the end result is the same:
function foodDemand(food){
console.log("I want to eat" + " " + food);
}
foodDemand("Apples");
89 comments
this was very helpful thank you
This made it so much Clearer!! thank you so much!
but where does the food demand come ??? i understand the funtion and basically inputin what you want to receve from its output, but when and where do i write …..foodDemand(“bacon”);….???
Hi Sheldon, once a function is created you can call the function as many times as you want. The only prerequisite is that you only call the function after the function exists, in other words, further down the page. You call it by writing what you just wrote: .foodDemand(“bacon”); OR foodDemand(“pizza”); etc. For each call the function executes and gives you an output. Does it make sense or did I miss your point?
Thanks a lot
@tony yeah that makes sense thanks man. I didt realize that you have to use the curly brakets for every new line of code even to call a function, but i got it finally after 2 days hahaha
you have to use curly brakets around the call.
{ foodDemand(“bacon”) }
No curly brackets around the the function call. Usually curly brackets are used to group code together. Fore example, iIn if(condition){ do this;} the brackets group the code and this code is part of the whole expression starting with if. The } ends the whole expression. in function x () { do this;} again, the brackets group the code which belongs to the function x.
Thank you Tony,this is really helpful
tony de araujo . YOU ARE AWESOME ty so much . you are inborn teacher ! / i mean you are great teacher who can explain hard things in simple way also in simple words
Incredibly helpful, what a wonderfully simple way to explain this. Much appreciated!
i still don’t get it, i put everything i have to put but it still does not work var foodDemand = function (food) { foodDemand(food) console.log(“i want to eat,” + “ “ + food); }; please tell me what’s wrong
Hi Jeremy, you sonsole.log has to be 100% exact. Your I is i and you have a comma after eat. Try to correct it to see what happens.
Thanks! Simple and nice.
Okay so i removed the coma , and i put the ‘i’ to an ‘I’ and it says i haven’t printed anything to the log, what does that mean ?
var foodDemand = function (food) { console.log(“I want to eat” + “ “ + food); foodDemand(“pizza”) };
just to show you i have done what you had exlained
Do you see the red notes on the very top. It says to move foodDemand(“pizza”); to outside of the function, at the bottom.
after the };
THANK YOU SO MUCH !!!!!!!!!!
thanx
Thanks , you saved my life
u are good
wow what a good explanation! you saved me a lot of time!
Awesome this really helps
Thanks
thanks man
thank you
best explanation ever.
thx
Great explanation! :)
It is saying that I am not naming my function.
@Garrett, please paste your code on the text box labeled “Add an answer”.
wow great response!
sweg
This was very helpful. Especially when I realized that
foodDemand=(“Fish”);
would work better as
foodDemand(“fish”);
:)
Fantastic way to break it down!!! thanks again. I also agree with Tommy’s comments above.
you should start teaching code full time
Thanks! I write all day long you can always find my publications at Amazon. As for Codecademy, I enjoy the interaction. This is a cool place to come in daily.
I am confused though, why did you make it a var?
why not just write it as
function foodDemand(food)
wouldn’t that work too?
They are just two different writing styles. The end result is the same. I’ve added your style to the end of my post (above)
When we use var it is a function being assigned as a value to a variable. This is very useful at times. Functions can be assigned as values: that is the real power because functions are used to save code that can be called at any time and anywhere.
Can someone post an amended/revised version of the above because I’m confused on where the Original Post mentions at the end PS this is an alternate way of writing the same function. I used that way though I’m confused on how there is an alternate way and where it is shown? :mindblownbyatomicbomb:
There are two ways to create a function. One is by doing it as shown on the last example, the other way is by assigning it to a variable like shown on the first example ( after the second image). For the most part, you use the one you like the most. They both give you the same end result. With practice you will get a feeling for which one to use at any given time.
For now, stick to the one shown by the exercise instructor and practice that one until you do it without thinking about it. Different instructors will have different preferences so you will see both throughout the course.
Ah ha, that makes sense…somehow I didn’t pick up on it/understand originally.
It makes more sense, to me at least, to drop/not primarily use the #1 option as it involves more text and I quite frankly don’t get why “var” is necessary. It just seems like I’m going the longer route you know? Like I’m doing extra work to get the same result. :mind hurts: 1.) var foodDemand = function(food)
2.) function foodDemand(food){
Thank you very much for your replies tony de araujo!
Hi William, the var is only necessary because in that example there is a variable declaration. The concept to take from the first example is that we can assign an anonymous function to almost anything on the program, rather than declaring a named function like it was done on style #2. You will see in more advanced code such as in jQuery, that anonymous functions are very practical and used everywhere. They are used to store code that can run when a certain condition becomes true. Let’s keep both styles in mind because they are both very useful in their own ways.
what if the parameter is empty instead will it be same like this “console.log(“I want to eat” + “ “ + food);” instead of like this “console.log(“I want to eat” + “ “ + );” #in my second case food is missing because the initial value of the function is nothing eg () but in the lesson we have (food).
which a value is already assingned.
Hi emmanuel I have answered the question on a separate message.
where is it @tony i am newbie. so i don’t have any idea :) thaks
For me it is all the way at the bottom of this page.
thanks @tony it help me a lot, could you tell me where can i get those arguments lesson i wanna know more about these so called arguments
Hi Emmanuel, those more advanced topics are covered on one of my eBooks “JavaScript Objects Functions and Arrays Explained”. I don’t mean to advertize my work but if you are interested just goggle the title.
thank you, you really have helped me
thanks a lot. this helped me as well..
Thank you Tony, for the very comprehensive answer!
I am having the same trouble. i wish when it tells you that you have a syntax error that it told you how to fix it.
this helps a ton
thank so much >_<
Excellent explanation. Seeing the visual of the function as a “box” helps me understand and remember better.
just checking to confirm what I did was correct…thanks for the visual…
that was long long, but worth to read! Thanks
Wonderful explanation.I can understand.
very long visual.. but thanks!
It doesn’t work, I just keep getting “I want to eat food” instead of what I put into “foodDemand”
Could you show your code?
yolo
itsays i didnt declare fooddemand but i did exactly what you showed
Make sure it is foodDemand ( second D is capitalized).
Thank’s, I forgot to put foodDemand(“Pizza”); at the end…
why do we put 2 ‘+’ symbols in the console.log code?
idk but my theory is that it adds adding so 1 + (adding) = 1,2,3,4,5,6,7,8…
you are adding “I want to eat”, a space (“ “), and “food” to form one thing.
Oops try again! “looks like you didn’t print anything to the console”
for example :) var foodDemand = function(food) { var food = food; console.log(“I want to eat” + “ “ + food) }; foodDemand(“meet”);
I can not figure out what is wrong with my code, its so frustrating. please help me, what do I miss? var foodDemand= function(food); { console.log(“I want to eat” + “ “ + food); } foodDemand(“Pasta”)
thanks
this guy is super smart!
Thank you Tony
Awesome!
You’re the man
awesome
What a concise, illustrative and delightful representation
super
Thanks I had the same problem
That’s such a great way to put functions!!
Very good explained !
after trying your code i am getting error
ReferenceError: foodDemand is not defined
Oops, try again. It looks like you didn’t put quotes around the food you gave foodDemand!
please help me
Thank you so much! Great way to explain functions. I’ve finally started to understand them.
Answer 54f04f8251b8876c0e0016d6
Having read through the above comments numerous times, gone back and forth adding, subtracting, cutting and pasting and trying to make my code work to no avail, (used exact copy of the code at the top of this forum topic - Edited by a moderator) I had to paste my code below in to http://www.jslint.com (JavaScript Code Quality Tool) just to check, and I have found that the code is riddled with errors! The CORRECT code - which is passed ready for the next lesson, yet still returns errors via jslint.com - is as follows:
My Code:
var foodDemand = function(food) { console.log(“i want to eat”+” “+ food); }; foodDemand (“Pizza”);
In short, there is no ‘=’ in the last line! ie: “foodDemand (“Pizza”);” and NOT “foodDemand = (“Pizza”);” In hindsight, this was quite a frustrating lesson & forum post, so I hope this is a lot more helpful for others ;-)
5 comments
Hi JYMIX, thanks for the heads up, I’ve edited the = issue on the top. As for passing the exercise, make sure your sentence is exactly as the one given by the instructor. copy/pasting is a good way to go. Like for example, instea do f i want should be “I want … “ The automatic validation algorithm is trying to match your sentence with the ones given by the author.
As I said, I did “exact copy of the code at the top of this forum topic”.. although it was wrong due to the ‘=’ .. so just as long as you guys get your code correct, then it makes life even more simpler for us learning ;-)
Thanks for posting this!
Yes, thank you for clarifying abut the = sign. I looked back and forth at the previous lesson (3/12) and couldn’t see anything. How did I possibly miss the added = sign!?!?
Thanks again!!!
When I did this, it said: “SyntaxError: missing ) after argument list” If you could explain this, that would be great. Thanks!
Answer 53e4ddd680ff339d580019cf
What was the point of the extra set of quotations inside the code between the plus signs? and why doesn’t the word “food” have to be in quotations? Thanks.
13 comments
The extra set of quotations is just a way to add a blank space. Sometimes we need to have a blank space by itself when we output something. In this case it doesn’t matter since we could have added the space in the previous set of quotation marks, but other times it makes a difference, like when the previous value is a variable. If for example food, the only way to add a space after it is by writing a set of quotation marks like: food + “ “+ . AS FOR the second question, if you refer to “Pizza”, Pizza is a string value and it needs to be in quotes otherwise JavaScript will throw an error because it does not recognize Pizza as a registered word in its current library.
that’s a valid question!
awsome!! thank :)
thanks this really helped
Thanks, that wasn’t explained!
Thank you Tony!
i still don’t understand :(
Which point exactly you don’t understand? Ask me a specific question and I will try to explain it if I can.
try changing your code and see what happens. console.log(“I want to eat” + “ “ + food); console.log(“I want to eat” + “ “ + “food”);
It has to be in quotations because it is a string. If it was just a number it wouldn’t need quotes
In thus case food is not a string, it is variable. No quotations allowed.
var foodDemand = function(food) { food = prompt(“what would you like to have”); console.log(“I want to eat” + “ “ + food); }; foodDeamnd(“food”);
i am getting reference error
belanagumouli did you try checking the spelling of foodDemand? In your example they are spelled differently.
Answer 54255b5f548c35bb13001fe0
How do you remember which function declaration gets a semicolon at the end of it and which one does not?
When we declare a variable we use the prefix var. All variable declarations or assignments terminate with a semicolon:
var name = “Huambo”;
var x = 33;
When we declare a function we use the prefix function:
function x () { return “hello world!”; }
Sometimes we assign a function as a value
to a variable:
var addMe = function(n1,n2) { return n1 + n2;};
var x = function () { return “hello world!”; };
The semicolon has nothing to do with the function, it has to do with the variable assignment.
=
Answer 5513362495e37861e400033c
Don’t know if anyone else had this problem but when I write the code out perfectly myself I get an error message but if I copy and paste it from somewhere and switch out the values for the ones I want it works.
1 comments
Based on my experience codeacademy seems to be checking the exact spelling and placement of all letters rather than the actual Javascript to confirm whether it has been done correctly. Several times the code will work fine on jsfiddle or the like but won’t work in the tutorials without error.
Answer 54832740d3292f598a000a89
Emmanuel asked this question: What if the parameter is empty?
Since I don’t know which parameter he is referring to I will show several possibilities that may answer several questions.
Example 1:
Calling the function without including a data argument (the one that replaces the input parameter):
function foodDemand(food){
console.log("I want to eat" + " " + food);
}
foodDemand();
I want to eat *undefined*
undefined is the original value given by JavaScript to parameter food.
Example 2:
Adding an argument data but no input parameter exists:
function foodDemand( ){
console.log("I want to eat" + " " + food);
}
foodDemand("pizza");
Output result: ReferenceError: food is not defined
A more advanced topic:
We can actually pass in data without having to write input parameters when we create the function:
Example 3:
Every function has an internal object called arguments. This object looks like an array and it can be addressed as location zero, 1,2 etc, like for example arguments[1] which is the second location of object arguments.
So we could use it to our benefit like this:
function foodDemand(){
console.log(“I want to eat” + “ “ + arguments[0]
);
}
foodDemand(“pizza”);
Output result: I want to eat pizza
Example 4:
Using multiple arguments:
function foodDemand(){
console.log("I want to eat" + " " + arguments[0]+ " and " + arguments[1]);
}
foodDemand("pizza", "chips");
Output result: I want to eat pizza and chips
We could also pass in variables instead of parameters. In this case we don’t need an input parameter and we don’t include the data when we call the function:
var food = "pizza";
function foodDemand(){
console.log("I want to eat" + " " + food);
}
foodDemand();
Output result: I want to eat pizza
5 comments
this is my code but when I enter it it gives me an error message that sais I did not declare foodDemand var foodDemand = function(food) { console.log(“I want to eat”+” “+ food); } foodDemand = (“Pizza”);
remove the = from foodDemand(“Pizza”);
ty
yw
+1
Answer 5522fb659113cb7c7400015f
var foodDemand = function(food) { console.log(“I want to eat” + “ “ + food) }; foodDemand(“pizza”)
1 comments
lol why the -1 vote? this one works XD
Answer 55729ccf76b8fe60d00007ce
Why can’t this be seen as: the function is the food preparation, the input is ingredients, the output is a meal. ingredients are variables, preparation the repeatable function. Meal is the output. I want to eat +” “+ meal. Or am I so far off, I’m on another planet.
Answer 5609c9e39113cb49ae0000c7
I think this question is broken on Firefox, for me it wouldn’t work and when I pasted the same code into Chrome- it was fine.
function foodDemand(food){
console.log(“I want to eat” + “ “ + food);
};
foodDemand(“Pizza”);
Answer 560a7e579113cb4b300000a1
This will work:
var foodDemand = function(food)
{console.log("I want to eat"+" "+ food)};
foodDemand ("INSERT FOOD HERE");
It will work just like that, but you can change the INSERT FOOD HERE to whatever you want.
Answer 54f076d195e378c871001ba7
Because I wanted extra practice, I downloaded an app to help quiz me and in it, it covered functions only the coding was different.
function luvMe () { alert (“One Million More Hugs to Go!”) } luvMe()
is this really acceptable? If so, how do i know when to use this way or the way taught here at code academy, does each one have a different use?
1 comments
Both styles are commonly used. Patrice both, in time you will develop a preference for one or the other but at times you will find that one makes more sense than the other (although there is not difference in the end result). It is just a matter of preference. There are subtle differences and I have written about it on several occasions here at Codecademy, but for all practical purposes, one is as good as the other. The differences concern more advanced code (google “variable hoisting”).
Answer 556535569376765ebc00032f
var foodDemand = function(food)
{
console.log(“I want to eat” + “ “ + food);
}
// Last hint: In your reusable block of code, end each line
foodDemand(“Hot Dog”);
Answer 556ba07d93767695b900058e
So….
foodDemand (“Pizza”) is good…. foodDemand = (“Pizza”) is not….
Got it…. Now…. Why is that? I want to understand what the code/computer is thinking.
1 comments
var foodDemand = function (name) { console.log(“I want a taco,” + “ “ + name); }; var name= “taco”; foodDemand ( name +” “+”I”+” “+”am”+” “+”hungry!”);
Answer 55729a8dd3292f02bc000679
What is the difference between function and subroutine?
Answer 5572a1bd93767633c80007d8
var greeting = function (name) This entire statement just seems backward to me.
To me it should logically read: using the variable name perform the function greeting. To me this reads: with the variable greeting perform the function name. Makes everything confusing. Seems var is out of place. If your are just trying to stand back and understand the flow of the algorithm.
Answer 55a42beae39efeb50a00013d
var foodDemand=function(food){
console.log("I want to eat"+" "+food);
}
foodDemand("some fish"); [just tree lines of code.]
Answer 55d92a2eb625482365000565
Why is that the the code is approved even if I remove semicolons? The directions say to always include a semicolon after each line.
Answer 55f6e2de937676ba57000729
Answer 5614c22ce39efe2b23000038
var foodDemand=function(“food”) { console.log(“I want to eat”+””+food); }; foodDemand(“Roti”);
It shows problem in syntax.
Answer 54aef1a476b8fe557100874e
2 comments
Hey Vinitius, Is that a good thing or a bad thing? :)))
extremely good
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons