7

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()
Gypsy Spellweaver
  • 5,425
  • 2
  • 17
  • 34
tazboy
  • 171
  • 2
  • 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. – Buffy Sep 28 '18 at 11:26
  • If you want the students to use parameters, give them an example where using a parameter is the obvious way to do it. – Michel Billaud Sep 28 '18 at 11:58
  • 3
    @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. – tazboy Sep 28 '18 at 13:26
  • @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) – Ben I. Sep 28 '18 at 13:45
  • 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. – Buffy Sep 28 '18 at 20:05
  • 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 – Buffy Sep 30 '18 at 14:09

4 Answers4

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) {
   ....
}
Michel Billaud
  • 937
  • 4
  • 10
  • 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? – tazboy Sep 30 '18 at 19:12
  • 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. – tazboy Sep 30 '18 at 19:16
  • @tazboy That's the reason why I chose drawings for the first examples. – Michel Billaud Sep 30 '18 at 19:40
  • Yeah, that's a great way to start. Did you do this in high school? How far do you go with them? – tazboy Sep 30 '18 at 19:46
  • 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. – Michel Billaud Sep 30 '18 at 19:52
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("tazboy")

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.

Peter
  • 9,082
  • 2
  • 22
  • 61
  • 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. – Buffy Sep 28 '18 at 11:45
  • 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. – tazboy Sep 28 '18 at 19:10
  • 1
    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. – Buffy Sep 28 '18 at 19:28
  • 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. – tazboy Sep 28 '18 at 22:57
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)
Adam
  • 269
  • 1
  • 5
  • 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/ – Buffy Sep 28 '18 at 14:56
  • @Buffy You can call them whatever you want; it is irrelevant to the question at hand. – Adam Sep 28 '18 at 14:58
  • You can also search on this site for "Pair Programming" to get a better sense of what it means. – Buffy Sep 28 '18 at 15:15
  • 1
    @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. – tazboy Sep 28 '18 at 19:07
  • 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. – Buffy Sep 28 '18 at 19:13
  • 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. – Buffy Sep 28 '18 at 19:18
  • It's not the short format. And it is irrelevant to the op question. – tazboy Sep 28 '18 at 22:08
  • @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. – Buffy Sep 30 '18 at 14:20
  • @Buffy well... wow – tazboy Sep 30 '18 at 19:18
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.

OBu
  • 594
  • 3
  • 15