Answers.Body: <p>Your example demonstrates <em>how</em> to declare and use a function with a parameter, but not <em>why</em> they should do so.</p>
  
  <p>Actually, there are no reason (for them) why they should bother with a new concept (function) instead of writing</p>
  
  <pre><code>name = input("Enter your name: ")
  print("Hello " + name)
  </code></pre>
  
  <p>which can be done with the programming elements they already know. And is both shorter and simpler, </p>
  
  <p>So, how to sell functions with parameters to beginners?</p>
  
  <ul>
  <li>they avoid code duplication (the example should contain several calls)</li>
  <li>they are called with different values (so parameters is the simplet way to transmit information)</li>
  <li>they implement a decomposition of tasks into subtasks (the sooner they practice the procedural approach, the better)</li>
  </ul>
  
  <p>Here is an example:</p>
  
  <pre><code>def draw_line(width, end, middle):
      line = end + middle*(width-2) + end;
      print(line)
  
  def draw_rect(height, width):
     draw_line(width, "+", "-")
     for  line in range(height-2):
        draw_line(width, "|", " ")
     draw_line(width, "+", "-")
  
  def draw_square(side):
      draw_rect(side, side)
  
  def main(args):
      draw_square(3)
      draw_square(5)
      draw_square(7)
  </code></pre>
  
  <p>Actually, I use Processing with a group of students. Most of them total beginners. The first motivation for functions is decomposition.</p>
  
  <p>After they draw a simple figure with a few predefined function calls, as <code>fill()</code> to select a fill color, <code>rect()</code> and <code>ellipse()</code>,  they are taught to split the work into user-defined functions</p>
  
  <pre><code>void draw() {
      draw_sky();
      draw_beach();
      draw_sunset();
  }
  </code></pre>
  
  <p>and parameters are then used for similar parts occurring at different places</p>
  
  <pre><code>void draw_seabird(int x, int y) {
     ....
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-30T19:12:26.500Z
Answers.Text: Do you do Processing in high school? I was thinking about teach p5.js but ended up with Python. Any issues or things you like?
Answers.UserDisplayName: tazboy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-30T19:16:24.297Z
Answers.Text: The example was just something simple I could put up for a talking point. Reuse and readability are talking points for sure. Decomposition is a lot easier when it comes to drawing.
Answers.UserDisplayName: tazboy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-30T19:40:01.843Z
Answers.Text: @tazboy That's the reason why I chose drawings for the first examples.
Answers.UserDisplayName: Michel Billaud
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-30T19:46:49.793Z
Answers.Text: Yeah, that's a great way to start. Did you do this in high school? How far do you go with them?
Answers.UserDisplayName: tazboy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-30T19:52:44.543Z
Answers.Text: That a one year professional training program at the university. Processing is used to explain the basics of programming and algorithmic, and then we switch to Java and other languages C#, php,  javascript etc.
Answers.UserDisplayName: Michel Billaud
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-29T17:56:15.173Z
Answers.LastActivityDate: 2018-09-30T09:03:39.190Z
Answers.LastEditDate: 2018-09-30T09:03:39.190Z
Answers.UserDisplayName: Michel Billaud
Answers.Body: <p>I teach Python after teaching students C, so my context might be slightly different. That said, my students get used to program execution beginning at <code>main()</code> from day one. You could take this as an opportunity to introduce the same approach in Python with the following:</p>
  
  <pre><code>def greeting(name):
      print("Hello, " + name)
  
  def main():
      greeting("tazboy")
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>If a student doesn't have a main function defined, they will see <code>NameError: name 'main' is not defined</code>.</p>
  
  <p>I will admit that the boilerplate at the bottom is about as opaque as you can get. Even so, boilerplate code is something they will encounter in many other programming languages. I correlate it for my students to the MLA heading they write at the top of any essay. It's something that just has to be there, and it has a clear and important purpose.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T11:45:12.110Z
Answers.Text: This answer is valid even if you _don't_ teach C first. Beginners don't actually need complete, bottom up, explanations of everything, especially a bit of boiler plate. It can be treated as a magic incantation. In the age of Harry Potter, especially.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T19:10:40.163Z
Answers.Text: I like this idea but I also don't want to add complexity to an awesomely, simple language like Python. Another part of this is my ignorance to the whole `__name__ == "__main__"`. I have no idea what that is, so I'm failing in that respect. Gotta go look that up now.
Answers.UserDisplayName: tazboy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T19:28:05.643Z
Answers.Text: Every language has its quirks @tazboy. Eventually you get used to that and just live with them. Alternatively, you can work to build better languages. But at some level a program has to connect to the system on which it runs. That results in a lot of those quirks. But for teaching, it is generally fine to ignore the details (while including them) until the student has a better overall grasp. The alternative is to tightly integrate a language and its OS as was done originally with Smalltalk and Oberon. You may think such necessary spells are "Riddikulus" and you'd be correct. Hard to avoid tho.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T22:57:10.470Z
Answers.Text: Yeah. I just need to teach that concept in a different way now, which is fine. This is  the first thing I encountered that I wasn't expecting and I'm really enjoying teaching Python. Thanks for info.
Answers.UserDisplayName: tazboy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T05:31:49.157Z
Answers.LastActivityDate: 2018-09-28T05:31:49.157Z
Answers.UserDisplayName: Peter
Answers.Body: <p>This answer is just to add an aspect to the existing answers:</p>
  
  <p>Don't blame the code given too much: It is a very good example if you want to explain how the python interpreter actually works! </p>
  
  <p>You could show the code and ask the students, whether <code>name</code> really is available in <code>greeting()</code> since <code>greeting</code> is defined at a point in time where the interpreter does no know anything abount <code>name</code>. Then show a step-by-step walkthrough in a visual debugger. </p>
  
  <p>Afterwards, you can explain why this is a really bad idea, e.g. by using different functions asking for names etc. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-29T14:49:14.060Z
Answers.LastActivityDate: 2018-10-29T14:49:14.060Z
Answers.UserDisplayName: OBu
Answers.Body: <p>I would think that any use of the same function with another parameter, stored in another variable, would solve the problem. For example, if they are pair programming:</p>
  
  <pre><code>def greeter(name):
    print("Hello, " + name)
  
  def pair_greeter(name_one, name_two):
    print(name_one + " and " + name_two + " are a great team!")
  
  typist_name = input("Who's the typist?")
  greeter(typist_name)
  observer_name = input("Who's the observer?")
  greeter(observer_name)
  pair_greeter(typist_name, observer_name)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T14:56:50.573Z
Answers.Text: For reasons that I hope are obvious to you, I would _much_ prefer _driver_ and _navigator_ to _typist_ and _observer_. Especially if you are speaking indirectly of pair programming. Your names give completely the wrong idea about pairing. You make one, the navigator, far too passive, for example. If you think that they are accurate names, I strongly suggest that you investigate further. https://www.amazon.com/Pair-Programming-Illuminated-Laurie-Williams/dp/0201745763/
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T14:58:23.473Z
Answers.Text: @Buffy You can call them whatever you want; it is irrelevant to the question at hand.
Answers.UserDisplayName: Adam
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T15:15:17.867Z
Answers.Text: You can also search on this site for "Pair Programming" to get a better sense of what it means.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T19:07:10.817Z
Answers.Text: @Buffy After reading some of your responses it seems as though you come off pretty aggressive. I want to hear your answers but I also don't want to because of how it's presented. You had a good suggestion to Adam... but ouch.
Answers.UserDisplayName: tazboy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T19:13:53.407Z
Answers.Text: I'm both an educator and an educator of educators. Names are important. If a name is misleading, don't use it. That is true both in programming and in education. I think it important to point that out, especially on this site. Names are not, in fact, irrelevant. You can also search on this site for "Naming", for example. One reason that agile development often fails in use is that the people trying it don't understand it and don't even try to apply it correctly. It can be just a buzzword to cover bad practice. Pair Programming means what it means.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T19:18:59.270Z
Answers.Text: What you interpret as aggressive is, sadly, a consequence of the short format of comments. I don't have room here for a complete explanation so am forced to give short summaries on occasion.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T22:08:10.383Z
Answers.Text: It's not the short format. And it is irrelevant to the op question.
Answers.UserDisplayName: tazboy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-30T14:20:11.287Z
Answers.Text: @tazboy, the general philosophy here is that the best answers should be useful to a future visitor, not just the OP. A mailing list is perfectly adequate for answering immediate questions. However, the fact that questions and answers here are preserved and searchable, as well as tagged, implies that there is a longer-term goal for this site. Future visitors probably won't have exactly the same problem, but if it is related, then the given answers might help them as well.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-30T19:18:24.773Z
Answers.Text: @Buffy well... wow
Answers.UserDisplayName: tazboy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T14:51:28.857Z
Answers.LastActivityDate: 2018-09-28T14:51:28.857Z
Answers.UserDisplayName: Adam
Body: <p>When teaching functions in Python I get a little annoyed that the code below actually prints out the greeting. I know the preferred way would be to pass in the name, but, for students, if it works then why do something more complex. I want to teach them how to pass in arguments but if they mess up and forget it will still work. Thoughts about how to approach situations like this without being convoluted or contrived?</p>
  
  <pre><code>def greeting():
      print("Hello " + name)
  
  
  name = input("Enter your name: ")
  greeting()
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-28T11:26:59.173Z
Comments.Text: You are being annoyed at the language itself, not the students. It is perfectly valid Python. Variable _name_ has a binding when _greeting_ is called. Use the code above as a lesson about dynamic binding, not a complaint against the student. Write your preferred code and contrast the two. It is an opportunity not a bug here.  I argue that the students have _not_ "messed up". They just didn't read your mind.
Comments.UserDisplayName: Buffy
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-28T11:58:04.050Z
Comments.Text: If you want the students to use parameters, give them an example where using a parameter is the obvious way to do it.
Comments.UserDisplayName: Michel Billaud
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-28T13:26:25.173Z
Comments.Text: @Buffy I didn't think anyone would imply that I'm annoyed by the students. I stated that it was the code, and I know it's not a bug.
Comments.UserDisplayName: tazboy
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-28T13:45:49.423Z
Comments.Text: @MichelBillaud Could you write up an answer with such an example?  It might help future visitors.  (I would try, but I am not even a little bit of a Pythoner)
Comments.UserDisplayName: Ben I.
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-28T20:05:09.857Z
Comments.Text: BTW, if you are trying to teach Python, but don't yet understand it thoroughly, have a look at this question: https://cseducators.stackexchange.com/questions/4379/how-do-you-teach-something-when-you-dont-know-it-yourself. It is a common enough dilemma, but there are solutions to it.
Comments.UserDisplayName: Buffy
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-30T14:09:43.410Z
Comments.Text: If you need to understand why the above happens, and how Python handles top-level statements in any module, see https://stackoverflow.com/questions/18138166/what-is-a-top-level-statement-in-python
Comments.UserDisplayName: Buffy
Tags: <lesson-ideas><python>
Title: Python function has access to global variables, and doesn't need passed arguments
ThreadText: # Python function has access to global variables, and doesn't need passed arguments
  
  (Asked by: username_0 on 2018-09-28)
  
  When teaching functions in Python I get a little annoyed that the code below actually prints out the greeting. I know the preferred way would be to pass in the name, but, for students, if it works then why do something more complex. I want to teach them how to pass in arguments but if they mess up and forget it will still work. Thoughts about how to approach situations like this without being convoluted or contrived?
  
  ```
  def greeting():
      print("Hello " + name)
  
  name = input("Enter your name: ")
  greeting()
  
  ```
  
  **Tags:** lesson-ideas, python
  
  > **Comments:**
  > **username_1**: You are being annoyed at the language itself, not the students. It is perfectly valid Python. Variable _name_ has a binding when _greeting_ is called. Use the code above as a lesson about dynamic binding, not a complaint against the student. Write your preferred code and contrast the two. It is an opportunity not a bug here.  I argue that the students have _not_ "messed up". They just didn't read your mind.  
  > **username_2**: If you want the students to use parameters, give them an example where using a parameter is the obvious way to do it.  
  > **username_0**: @username_1 I didn't think anyone would imply that I'm annoyed by the students. I stated that it was the code, and I know it's not a bug.  
  > **username_3**: @username_2 Could you write up an answer with such an example?  It might help future visitors.  (I would try, but I am not even a little bit of a Pythoner)  
  > **username_1**: BTW, if you are trying to teach Python, but don't yet understand it thoroughly, have a look at this question: https://cseducators.stackexchange.com/questions/4379/how-do-you-teach-something-when-you-dont-know-it-yourself. It is a common enough dilemma, but there are solutions to it.  
  > **username_1**: If you need to understand why the above happens, and how Python handles top-level statements in any module, see https://stackoverflow.com/questions/18138166/what-is-a-top-level-statement-in-python  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_2 on 2018-09-29. Score: 4
  
  Your example demonstrates *how* to declare and use a function with a parameter, but not *why* they should do so.
  
  Actually, there are no reason (for them) why they should bother with a new concept (function) instead of writing
  
  ```
  name = input("Enter your name: ")
  print("Hello " + name)
  
  ```
  
  which can be done with the programming elements they already know. And is both shorter and simpler,
  
  So, how to sell functions with parameters to beginners?
  
  * they avoid code duplication (the example should contain several calls)
  * they are called with different values (so parameters is the simplet way to transmit information)
  * they implement a decomposition of tasks into subtasks (the sooner they practice the procedural approach, the better)
  
  Here is an example:
  
  ```
  def draw_line(width, end, middle):
      line = end + middle*(width-2) + end;
      print(line)
  
  def draw_rect(height, width):
     draw_line(width, "+", "-")
     for  line in range(height-2):
        draw_line(width, "|", " ")
     draw_line(width, "+", "-")
  
  def draw_square(side):
      draw_rect(side, side)
  
  def main(args):
      draw_square(3)
      draw_square(5)
      draw_square(7)
  
  ```
  
  Actually, I use Processing with a group of students. Most of them total beginners. The first motivation for functions is decomposition.
  
  After they draw a simple figure with a few predefined function calls, as `fill()` to select a fill color, `rect()` and `ellipse()`, they are taught to split the work into user-defined functions
  
  ```
  void draw() {
      draw_sky();
      draw_beach();
      draw_sunset();
  }
  
  ```
  
  and parameters are then used for similar parts occurring at different places
  
  ```
  void draw_seabird(int x, int y) {
     ....
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Do you do Processing in high school? I was thinking about teach p5.js but ended up with Python. Any issues or things you like?  
  > **username_0**: The example was just something simple I could put up for a talking point. Reuse and readability are talking points for sure. Decomposition is a lot easier when it comes to drawing.  
  > **username_2**: @username_0 That's the reason why I chose drawings for the first examples.  
  > **username_0**: Yeah, that's a great way to start. Did you do this in high school? How far do you go with them?  
  > **username_2**: That a one year professional training program at the university. Processing is used to explain the basics of programming and algorithmic, and then we switch to Java and other languages C#, php,  javascript etc.  
  
  ---
  
  ### Answer by username_4 on 2018-09-28. Score: 3
  
  I teach Python after teaching students C, so my context might be slightly different. That said, my students get used to program execution beginning at `main()` from day one. You could take this as an opportunity to introduce the same approach in Python with the following:
  
  ```
  def greeting(name):
      print("Hello, " + name)
  
  def main():
      greeting("username_0")
  
  if __name__ == "__main__":
      main()
  
  ```
  
  If a student doesn't have a main function defined, they will see `NameError: name 'main' is not defined`.
  
  I will admit that the boilerplate at the bottom is about as opaque as you can get. Even so, boilerplate code is something they will encounter in many other programming languages. I correlate it for my students to the MLA heading they write at the top of any essay. It's something that just has to be there, and it has a clear and important purpose.
  
  > **Comments:**
  > **username_1**: This answer is valid even if you _don't_ teach C first. Beginners don't actually need complete, bottom up, explanations of everything, especially a bit of boiler plate. It can be treated as a magic incantation. In the age of Harry Potter, especially.  
  > **username_0**: I like this idea but I also don't want to add complexity to an awesomely, simple language like Python. Another part of this is my ignorance to the whole `__name__ == "__main__"`. I have no idea what that is, so I'm failing in that respect. Gotta go look that up now.  
  > **username_1**: Every language has its quirks @username_0. Eventually you get used to that and just live with them. Alternatively, you can work to build better languages. But at some level a program has to connect to the system on which it runs. That results in a lot of those quirks. But for teaching, it is generally fine to ignore the details (while including them) until the student has a better overall grasp. The alternative is to tightly integrate a language and its OS as was done originally with Smalltalk and Oberon. You may think such necessary spells are "Riddikulus" and you'd be correct. Hard to avoid tho.  
  > **username_0**: Yeah. I just need to teach that concept in a different way now, which is fine. This is  the first thing I encountered that I wasn't expecting and I'm really enjoying teaching Python. Thanks for info.  
  
  ---
  
  ### Answer by username_5 on 2018-10-29. Score: 0
  
  This answer is just to add an aspect to the existing answers:
  
  Don't blame the code given too much: It is a very good example if you want to explain how the python interpreter actually works!
  
  You could show the code and ask the students, whether `name` really is available in `greeting()` since `greeting` is defined at a point in time where the interpreter does no know anything abount `name`. Then show a step-by-step walkthrough in a visual debugger.
  
  Afterwards, you can explain why this is a really bad idea, e.g. by using different functions asking for names etc.
  
  
  ---
  
  ### Answer by username_6 on 2018-09-28. Score: 0
  
  I would think that any use of the same function with another parameter, stored in another variable, would solve the problem. For example, if they are pair programming:
  
  ```
  def greeter(name):
    print("Hello, " + name)
  
  def pair_greeter(name_one, name_two):
    print(name_one + " and " + name_two + " are a great team!")
  
  typist_name = input("Who's the typist?")
  greeter(typist_name)
  observer_name = input("Who's the observer?")
  greeter(observer_name)
  pair_greeter(typist_name, observer_name)
  
  ```
  
  > **Comments:**
  > **username_1**: For reasons that I hope are obvious to you, I would _much_ prefer _driver_ and _navigator_ to _typist_ and _observer_. Especially if you are speaking indirectly of pair programming. Your names give completely the wrong idea about pairing. You make one, the navigator, far too passive, for example. If you think that they are accurate names, I strongly suggest that you investigate further. https://www.amazon.com/Pair-Programming-Illuminated-Laurie-Williams/dp/0201745763/  
  > **username_6**: @username_1 You can call them whatever you want; it is irrelevant to the question at hand.  
  > **username_1**: You can also search on this site for "Pair Programming" to get a better sense of what it means.  
  > **username_0**: @username_1 After reading some of your responses it seems as though you come off pretty aggressive. I want to hear your answers but I also don't want to because of how it's presented. You had a good suggestion to username_6... but ouch.  
  > **username_1**: I'm both an educator and an educator of educators. Names are important. If a name is misleading, don't use it. That is true both in programming and in education. I think it important to point that out, especially on this site. Names are not, in fact, irrelevant. You can also search on this site for "Naming", for example. One reason that agile development often fails in use is that the people trying it don't understand it and don't even try to apply it correctly. It can be just a buzzword to cover bad practice. Pair Programming means what it means.  
  > **username_1**: What you interpret as aggressive is, sadly, a consequence of the short format of comments. I don't have room here for a complete explanation so am forced to give short summaries on occasion.  
  > **username_0**: It's not the short format. And it is irrelevant to the op question.  
  > **username_1**: @username_0, the general philosophy here is that the best answers should be useful to a future visitor, not just the OP. A mailing list is perfectly adequate for answering immediate questions. However, the fact that questions and answers here are preserved and searchable, as well as tagged, implies that there is a longer-term goal for this site. Future visitors probably won't have exactly the same problem, but if it is related, then the given answers might help them as well.  
  > **username_0**: @username_1 well... wow  
  
  ---
  
  