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: ")
  • $\begingroup$ 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. $\endgroup$
    – Buffy
    Sep 28, 2018 at 11:26
  • $\begingroup$ If you want the students to use parameters, give them an example where using a parameter is the obvious way to do it. $\endgroup$ Sep 28, 2018 at 11:58
  • 3
    $\begingroup$ @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. $\endgroup$
    – tazboy
    Sep 28, 2018 at 13:26
  • $\begingroup$ @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) $\endgroup$
    – Ben I.
    Sep 28, 2018 at 13:45
  • $\begingroup$ BTW, if you are trying to teach Python, but don't yet understand it thoroughly, have a look at this question: cseducators.stackexchange.com/questions/4379/…. It is a common enough dilemma, but there are solutions to it. $\endgroup$
    – Buffy
    Sep 28, 2018 at 20:05

4 Answers 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;

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):

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() {

and parameters are then used for similar parts occurring at different places

void draw_seabird(int x, int y) {
  • $\begingroup$ 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? $\endgroup$
    – tazboy
    Sep 30, 2018 at 19:12
  • $\begingroup$ 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. $\endgroup$
    – tazboy
    Sep 30, 2018 at 19:16
  • $\begingroup$ @tazboy That's the reason why I chose drawings for the first examples. $\endgroup$ Sep 30, 2018 at 19:40
  • $\begingroup$ Yeah, that's a great way to start. Did you do this in high school? How far do you go with them? $\endgroup$
    – tazboy
    Sep 30, 2018 at 19:46
  • $\begingroup$ 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. $\endgroup$ Sep 30, 2018 at 19:52

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():

if __name__ == "__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.

  • 1
    $\begingroup$ 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. $\endgroup$
    – Buffy
    Sep 28, 2018 at 11:45
  • $\begingroup$ 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. $\endgroup$
    – tazboy
    Sep 28, 2018 at 19:10
  • 1
    $\begingroup$ 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. $\endgroup$
    – Buffy
    Sep 28, 2018 at 19:28
  • $\begingroup$ 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. $\endgroup$
    – tazboy
    Sep 28, 2018 at 22:57

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?")
observer_name = input("Who's the observer?")
pair_greeter(typist_name, observer_name)
  • $\begingroup$ 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. amazon.com/Pair-Programming-Illuminated-Laurie-Williams/dp/… $\endgroup$
    – Buffy
    Sep 28, 2018 at 14:56
  • $\begingroup$ @Buffy You can call them whatever you want; it is irrelevant to the question at hand. $\endgroup$
    – Adam
    Sep 28, 2018 at 14:58
  • $\begingroup$ You can also search on this site for "Pair Programming" to get a better sense of what it means. $\endgroup$
    – Buffy
    Sep 28, 2018 at 15:15
  • 1
    $\begingroup$ @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. $\endgroup$
    – tazboy
    Sep 28, 2018 at 19:07
  • $\begingroup$ 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. $\endgroup$
    – Buffy
    Sep 28, 2018 at 19:13

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.