English subtitles

← 05-45 Calling Functions

dummy description

Get Embed Code
3 Languages

Showing Revision 1 created 05/13/2012 by Amara Bot.

  1. So now we know how to call functions and return from functions,
  2. and this makes me doubly happy.
  3. But both of those things assume we have a function somewhere,
  4. which we currently don't.
  5. We don't know how to make them.
  6. So right now, very soon, we're going to get into defining functions,
  7. making new functions out of nothing.
  8. Here I've defined the same function in Python on the left and JavaScript on the right.
  9. And while the syntax changes, we spell the words differently,
  10. all the key concepts are there.
  11. We have to list the name of the function, fname,
  12. we have to list the formal parameters, fparam in our previous example,
  13. and we have to give the body.
  14. And we said we'd just make a tuple, a for tuple that has the word "function" as a string.
  15. So if the user writes in code like this and we're making our JavaScript interpreter
  16. for our web browser, what should we turn it into?
  17. Well, a function value that has 4 parts:
  18. the word "function," telling us that it's something we can call;
  19. the list of parameters, x, maybe y and z;
  20. the body, a list of statements;
  21. the environment we were in when the function was defined.
  22. We don't need the function name because we'll be adding a mapping
  23. from the name to this value in the environment--
  24. to the old environment, that is, not this new one. To the previous one.