English subtitles

← Using Procedures - Intro to Computer Science

Get Embed Code
8 Languages

Showing Revision 7 created 05/25/2016 by Udacity Robot.

  1. Unfortunately, we're not quite ready to be all smiley.
  2. Sebastian tells me it's going to be a few years before I can get
  3. my own self-driving car, but the bigger problem is we haven't yet talked about
  4. how to actually use the procedure.
  5. All we've done is make them; until we can actually use them
  6. we don't have a good reason to be really happy yet.
  7. We're going to learn how to use them next, then we'll be back to being smiley.
  8. Now we are going to learn how to use a procedure.
  9. The way to use a procedureâÂÂwe need the name of the procedure, followed by
  10. a left paren, followed by a list of inputs.
  11. There could be any number of inputs, but it has to match the number of inputs
  12. the procedure expects.
  13. These inputs are sometimes called "operands."
  14. They are sometimes called "arguments."
  15. We're not going to argue about that.
  16. We're just going to call them inputs.
  17. You have actually already done something quite similar to this.
  18. Back in Unit 1, you learned about using "find" on "strings."
  19. With "find" you would pass in one or two inputs.
  20. The first input was a "string."
  21. That was the string you are looking forâÂÂthat's the first inputâÂÂ
  22. and the second input could be a numberâÂÂthe position where you start
  23. looking for that string.
  24. We use "find" in many ways in Unit 1, as well as you used it
  25. yourself in the homework for Unit 1.
  26. "Find" is a little different from the procedures that you define yourself.
  27. First of all, it's built in.
  28. The other thing that was different is that, instead of just having "find," we had
  29. another input that was really over here.
  30. We have the string that we were doing the "find" inâÂÂlet's say it was
  31. in the variable pageâÂÂthat's really another input to find.
  32. We'll talk in a later class about why that's done differently, but it's
  33. very similar to calling a procedure where one of the inputs is over hereâÂÂ
  34. the other two are here.
  35. It's a little different from that and we won't get into that in this course but
  36. in a later course you'll learn more about what this really means.
  37. For all the procedures that you define yourself, we won't have any
  38. object to invoke them on.
  39. We'll just have the procedure to call and the arguments or operands or inputsâÂÂ
  40. as you like to call themâÂÂto pass in.
  41. Let's see how that works with a simple procedure.
  42. I am going to define the procedure "rest_of_string," and we'll give it the
  43. parameter "s," so that means it takes one input and we are going to use the
  44. name "s" to refer to the value of that input.
  45. We'll make it "return to string" from the first character to the end.
  46. We will use the "string" indexing operator "return s [1:]."
  47. This will evaluate to the "string," with the first letter removed, so all "strings" from
  48. position 1 until the end of the "string."
  49. That's what we return.
  50. So, the output of "rest_of_string" is that new string that starts from
  51. the second letter in the input "string."
  52. Here's an example of how to use this procedure.
  53. We could call it directly.
  54. We could say "print rest_of_string."
  55. That's our procedure.
  56. Now we are going to have our paren and we are going to pass in an input.
  57. There's one parameter to "rest_of_string," so we need one input
  58. to pass in and it should be a "string."
  59. We'll pass in the string 'audacity'.
  60. What happens when we call a procedure like this, execution will jump
  61. into the body of the procedure.
  62. We can think of what the interpreter is doing now.
  63. Instead of running the code here, the interpreter will move.
  64. When we call a procedure, it will jump to run the code inside the procedure.
  65. It will assign to the parameters the values passed in as the inputs.
  66. We can think of this as there being an assignment that says now the value of
  67. "s" is the value of this input that was passed in.
  68. Now we are going to evaluate the body of the procedure.
  69. In this case there's only one statementâÂÂit's this return statement.
  70. We are going to find this value, so s [1:].
  71. The result of that is going to be the string 'udacity'.
  72. Then we got to the return.
  73. What return means is we're going to jump back.
  74. We're jumping back to where we called the procedure, but now we actually
  75. have a result.
  76. When we jump back, the value that this evaluates to is whatever value we
  77. returned.
  78. In this case, it's the string 'udacity.'
  79. So we don't have our self driving car,
  80. but now you can define and use procedures.
  81. This is a really powerful concept
  82. Anything that we are going to do in the rest of the course and anything
  83. almost anyone does in programming computers is all about defining
  84. procedures and using procedures.
  85. Now we should have a big smile.
  86. We can think of our procedures in terms of mapping inputs to outputs.
  87. We can think of our humans as also mapping inputs to outputs.
  88. We have inputs coming in through the eyes, through the
  89. mouthâÂÂmaybe we even have a nose.
  90. I won't try to draw any of the outputs of our human procedure, but since
  91. procedures are such an important concept, we are going to have several
  92. quizzes now to check that you understand them well.