How Functions Work

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 896 Views - Last Post: 05 September 2013 - 10:09 PM

#1 Deygus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-June 10

How Functions Work

Posted 04 September 2013 - 12:50 PM

So normally I try my hardest to learn things on my own as I love learning now things but I'm totally new and in the Process of learning Javascript Functions and how to call them but I don't think I'm understanding it correctly. I looked around on the internet but it's still confusing me after reading a few different examples from various sites about this which were all different and that only confused me even more since some of the examples had parts that I don't completely understand yet.

I was following a tutorial which helped me to write this simple function to call and learn:

var foodDemand = function (food) {
    console.log("I want to eat" + " " + food);
};

foodDemand (food);



It's the same as a previous example that was used in the lesson prior to the one I'm on now in the tutorial. When I was following yet it's telling me it's wrong and that it looks like I didn't put quotes around " food " that I gave foodDemand. Yet it's identical to the previous example in the Tutorial which worked and this one didn't. The only difference that I can see is the var name and the function name. I don't expect the answer but I just want to know what I'm missing or doing wrong so that I can understand and fix it better. The only other thing I could think of is that maybe there's a problem with the Testing Tool I was using to run it but i'm not sure where or what a good javascript testing tool would be if there are any that I should get. Any tips about this are appreciated, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How Functions Work

#2 JoeSimmons  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 10-August 13

Re: How Functions Work

Posted 04 September 2013 - 01:06 PM

Ah. Welcome.

You tried to call 'foodDemand' with a variable you didn't define yet.

You have to supply it a value, like so:
foodDemand('steak');

Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: How Functions Work

Posted 04 September 2013 - 01:07 PM

Quote

When I was following yet it's telling me it's wrong and that it looks like I didn't put quotes around " food " that I gave foodDemand.

in your code you have two very different variables.

the one in your function (lines #1 and #2) is a function parameter. (and it overwrites any global variables named food)

the one in the function call (line #4) is a regular variable thatís passed into the function. (and has incidentally the same name as the parameter, which is an added bonus of confusion) so it would only make sense if you use it like
var food = "meat";
foodDemand(food);

of course the second food may as well be the string "food", but then youíre really missing the string quotes.
Was This Post Helpful? 3
  • +
  • -

#4 Deygus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-June 10

Re: How Functions Work

Posted 04 September 2013 - 01:12 PM

View PostJoeSimmons, on 04 September 2013 - 01:06 PM, said:

Ah. Welcome.

You tried to call 'foodDemand' with a variable you didn't define yet.

You have to supply it a value, like so:
foodDemand('steak');


Oh okay I think I see what you mean now. I think what was confusing me is why a different food name after the " foodDemand " call such as 'steak' had to be different from what I wrote above for it to work.
Was This Post Helpful? 0
  • +
  • -

#5 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,929
  • Joined: 30-April 10

Re: How Functions Work

Posted 04 September 2013 - 01:12 PM

Here is a link that may help you from devdocs.io

The main thing you need to know is that you are passing an argument to the function, the argument you passed is a variable that is undefined. as stated above you will need to either define the food variable with a string or object or just pass a string as the argument.
Was This Post Helpful? 1
  • +
  • -

#6 Deygus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-June 10

Re: How Functions Work

Posted 04 September 2013 - 01:17 PM

View PostDormilich, on 04 September 2013 - 01:07 PM, said:

Quote

When I was following yet it's telling me it's wrong and that it looks like I didn't put quotes around " food " that I gave foodDemand.

in your code you have two very different variables.

the one in your function (lines #1 and #2) is a function parameter. (and it overwrites any global variables named food)

the one in the function call (line #4) is a regular variable thatís passed into the function. (and has incidentally the same name as the parameter, which is an added bonus of confusion) so it would only make sense if you use it like
var food = "meat";
foodDemand(food);

of course the second food may as well be the string "food", but then youíre really missing the string quotes.


The tutorial had me writing it that way. I know there's various ways but it was probably just causing further confusion to me because of that and the Hint for the Tutorial I was working on wasn't very helpful at all.

Thanks for breaking all this down more for me.
Was This Post Helpful? 0
  • +
  • -

#7 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,929
  • Joined: 30-April 10

Re: How Functions Work

Posted 04 September 2013 - 01:19 PM

This is a good tutorial on functions at javascript.info there are other links in my signature below that may help.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: How Functions Work

Posted 04 September 2013 - 01:23 PM

View PostDormilich, on 04 September 2013 - 03:07 PM, said:

the one in your function (lines #1 and #2) is a function parameter. (and it overwrites any global variables named food)


Just for clarity, "overwrites" should read "shadows" here - the global variable continues to have its value, but in the local context there is now a variable called food which is not related to global one, and does not necessarily share its value. (one way that it might share the same value would be if you were to provide the global variable as an argument to the function)

> var foo = "7"
> var bar = function (foo) { console.log("foo = " + " " + foo);}
> bar (8)
foo =  8
> bar (foo)
foo =  7


This post has been edited by jon.kiparsky: 04 September 2013 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#9 Deygus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-June 10

Re: How Functions Work

Posted 04 September 2013 - 01:24 PM

View Postlaytonsdad, on 04 September 2013 - 01:12 PM, said:

Here is a link that may help you from devdocs.io

The main thing you need to know is that you are passing an argument to the function, the argument you passed is a variable that is undefined. as stated above you will need to either define the food variable with a string or object or just pass a string as the argument.



Okay cool, thank you and that looks useful and like something I could probably follow decently. My issue is that I'm very visual so I'm learning and finding that I tend to get bored and start to daze out with too much lengthy reading with not a lot of examples or good interactivity to teach me. Otherwise I get confused and frustrated over it all trying to figure it out so I'm trying to find ways that I can learn this appropriately without getting too overwhelmed.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: How Functions Work

Posted 04 September 2013 - 01:27 PM

I suggest you develop an attention span. Artificially limiting yourself to the most inefficient mode of acquiring information is a sure route to failure.
Was This Post Helpful? 1
  • +
  • -

#11 Deygus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-June 10

Re: How Functions Work

Posted 04 September 2013 - 01:32 PM

View Postjon.kiparsky, on 04 September 2013 - 01:23 PM, said:

View PostDormilich, on 04 September 2013 - 03:07 PM, said:

the one in your function (lines #1 and #2) is a function parameter. (and it overwrites any global variables named food)


Just for clarity, "overwrites" should read "shadows" here - the global variable continues to have its value, but in the local context there is now a variable called food which is not related to global one, and does not necessarily share its value. (one way that it might share the same value would be if you were to provide the global variable as an argument to the function)

> var foo = "7"
> var bar = function (foo) { console.log("foo = " + " " + foo);}
> bar (8)
foo =  8
> bar (foo)
foo =  7




I'm not quite sure I understand doing it that way fully yet but as I learn more it'll become much clearer. I plan to learn as much of this as possible one way or another. It just gets very overwhelming for me right now a lot of the time.

View Postjon.kiparsky, on 04 September 2013 - 01:27 PM, said:

I suggest you develop an attention span. Artificially limiting yourself to the most inefficient mode of acquiring information is a sure route to failure.



Yeah I know, you have a very valid point there. Like I said, I've gotta figure it out one way or another. I don't want to do anything to fail at this so i've basically been forcing myself to try to read and do more without getting a short attention span over it all. What i've been doing is looking at some tutorials and then making it interactive by attempting to recreate it myself to learn it.
Was This Post Helpful? 0
  • +
  • -

#12 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,929
  • Joined: 30-April 10

Re: How Functions Work

Posted 04 September 2013 - 01:34 PM

I think if you take a look at my link from javascript.info you will get what you are looking for.
Was This Post Helpful? 1
  • +
  • -

#13 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,929
  • Joined: 30-April 10

Re: How Functions Work

Posted 04 September 2013 - 01:52 PM

Quote

My issue is that I'm very visual so I'm learning and finding that I tend to get bored and start to daze out with too much lengthy reading with not a lot of examples or good interactivity to teach me. Otherwise I get confused and frustrated over it all trying to figure it out so I'm trying to find ways that I can learn this appropriately without getting too overwhelmed.


I am the same way, I use my passion for learning to get me through the rough times when learning something new. I am very passionate about learning anything I don't know and that is what drives me to spend most of my time reading new things and watching videos when I am bored if reading.

You can overcome anything if you want it bad enough. Keep working and it will pay off.
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7999
  • View blog
  • Posts: 13,701
  • Joined: 19-March 11

Re: How Functions Work

Posted 04 September 2013 - 02:58 PM

View PostDeygus, on 04 September 2013 - 03:32 PM, said:

I'm not quite sure I understand doing it that way fully yet but as I learn more it'll become much clearer. I plan to learn as much of this as possible one way or another. It just gets very overwhelming for me right now a lot of the time.


It's just a small correction to the previous post - the point is, that you're not overwriting a variable, you're hiding it for the duration of the function call.

Quote

Yeah I know, you have a very valid point there. Like I said, I've gotta figure it out one way or another. I don't want to do anything to fail at this so i've basically been forcing myself to try to read and do more without getting a short attention span over it all. What i've been doing is looking at some tutorials and then making it interactive by attempting to recreate it myself to learn it.


Definitely something to work on. I suggest you try reading an article in sections. After each section, work through all of the code examples from that section - make sure you understand what's going on in there - then get up and spend two to five minutes not sitting down - not more, not less. Repeat until article is finished. That way you get into a rhythm with built-in breaks, which might help.

This post has been edited by laytonsdad: 05 September 2013 - 11:25 AM
Reason for edit:: fixed quote

Was This Post Helpful? 0
  • +
  • -

#15 JoeSimmons  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 10-August 13

Re: How Functions Work

Posted 05 September 2013 - 11:07 AM

Since you're a visual learner, might I suggest you look at a few tutorials on YouTube. I don't know if they have exactly what you want, but there's some decent stuff there.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2