Answers.Body: <p>I think the example you showed on the original question is more about exception handling and unit testing rather than explaining the concept of function.</p>
  
  <p>You may want to teach students on explaining the concept of function in a very simple way such as "Why is a lego so successful? Because it uses blocks! Function is like a lego block. It's about reusability, modular programming.", and give them enough time (maybe several weeks or so) to practice with different kind of examples to naturally get familiar with using functions.</p>
  
  <p>The error-handling and unit-testing are more advanced concept and technique. It's probably best to teach these concepts once students are pretty good at writing code in general. I would suggest to actually teach this almost at the end of the course. It's important in the real-world software development, but for learning, I think these are less important compared to other topics.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-19T23:50:23.643Z
Answers.LastActivityDate: 2018-11-19T23:50:23.643Z
Answers.UserDisplayName: Jason Huh
Answers.Body: <p>You can call the function using the <a href="https://docs.python.org/3/library/unittest.html" rel="nofollow noreferrer">python unit test framework</a> and run different tests against it. Simple print-statements and other things will most probably no longer work.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-27T01:18:23.530Z
Answers.Text: Exactly, I use pytest though, it's simpler. pytest errors out if there is unexpected output when importing the module. The  problem is not in detecting their mistakes, those are pretty obvious, the real problem is that they persistently don't get it!
Answers.UserDisplayName: Louis B.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-26T20:21:58.767Z
Answers.LastActivityDate: 2018-10-26T20:21:58.767Z
Answers.UserDisplayName: OBu
Answers.Body: <p>If classes are guns, functions are sort of like bullets. Not a guns take the same rounds, not all classes have the same functions. But some classes can have the same base classes therefore have some similar functions; some guns have the same manufacturer and take the same bullets. Instances of a class can call functions, just as a gun can shoot bullets. Maybe thats why call of duty calls it "create a class"...</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-06T20:44:06.290Z
Answers.LastActivityDate: 2018-11-06T20:44:06.290Z
Answers.UserDisplayName: Mike
Answers.Body: <p>Instead of an example, use a metaphor. Get yourself a frisbee - ultimate disk and two packs of sticky notes. You are the caller of a function. Another person is the function itself. Write a value on the sticky note and stick it to the frisbee. The <em>pass</em> it to the function (person). The person catches the disk, and replaces the stick note with a new one with a new computed <em>function</em> value. They then <em>return</em> the disk to you. You get the computed value and announce it. </p>
  
  <p>Without the return value you get nothing. You can use any function you like but zero or one arguments is the easiest to manage.</p>
  
  <p>A slightly more sophisticated version can be used to discuss pass by value and pass by reference. </p>
  
  <p>Due to Mike Clancy of UCBerkeley. It is a classic from many years ago. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-27T01:13:27.627Z
Answers.Text: I've tried something like that but without the physical object. Scenario: we're both working late in the electronics lab and I forgot my calculator. I call out to you, Hey Fred, what's the square root of 4? You return the result of the computation, It's 2! I called the function by name, Fred, passed a value and waited for a returned value. The function didn't care what problem I was solving, and I the caller didn't care how the answer was computed.
Answers.UserDisplayName: Louis B.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-18T16:20:15.037Z
Answers.Text: That's a great suggestion @Buffy!  Thanks for sharing that answer!
Answers.UserDisplayName: Onorio Catenacci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-27T00:53:37.363Z
Answers.LastActivityDate: 2018-10-27T00:53:37.363Z
Answers.UserDisplayName: Buffy
Answers.Body: <p>I saw the light at the end of the fiber optic cable as soon as you said that these are students of electronics. There is nothing more modular and encapsulated than that! Software is simply electronics without the hardware part. </p>
  
  <p>If you said to them, "What are the building blocks of a simple AM radio receiver?" they would (hopefully) reply something like: antenna, tuning, rectification, filter, audio output... A crystal radio has all of those. They already know that it is pointless to build the antenna in to the filter, or duplicate the tuning parts more than once. They know that they can work in teams to produce parts that fit together to make a complete radio.</p>
  
  <p>So speak to them in a language they already know: the functional relationship of parts and wholes. Using globals instead of parameters would be like having extra cables going outside the radio to connect different parts of it to the signal path or the power supply. Can you say "Rube Goldberg"? I knew you could. </p>
  
  <p>Man, let me teach them. I have been studying electronics on my own since before anyone I knew even had seen a computer. Electronics students should be the easiest audience in the world to teach programming to. Use examples they already know.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-27T14:39:01.587Z
Answers.Text: I am sure to draw fire from the community by talking about *Software* in terms of real-world things, instead of explaining it as a closed, hermetic universe entirely consisting of concepts unrelated to anything the students have encountered before.
Answers.UserDisplayName: Scott Rowe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-28T18:59:34.863Z
Answers.Text: Bingo! And no, you won't get flak from me for mixing electronics and software. I deal in embedded systems and a system diagram can be any of pure hardware, pure software or a mix (e.g. state machine, FPGA).
Answers.UserDisplayName: Louis B.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-27T14:35:36.690Z
Answers.LastActivityDate: 2018-10-27T14:35:36.690Z
Answers.UserDisplayName: Scott Rowe
Answers.Body: <p>I like to think about pure function as a transmutation of inputs. Just like in Alchemy you connect Fire and Water to get Steam. Fire and Water are inputs, Steam is output. Or treat function as an assembly machine -- it takes resources, and converts those into another resource.</p>
  <p>After that try posing a question: given two integer numbers as an input, what kinds of transmutation about these two numbers can you think about?</p>
  <p>So first idea should be:</p>
  <ul>
  <li>add numbers to get a number</li>
  <li>multiply numbers</li>
  <li>substract numbers</li>
  <li>divide numbers</li>
  </ul>
  <p>And usually that's it, I mean, student fantasy often stops here. Now your turn to make &quot;how did I not think about it?&quot; in student's brain:</p>
  <ul>
  <li>substract first number from second (change argument order)</li>
  <li>modulo divide two numbers</li>
  <li>integer part of division</li>
  <li>exponentiate numbers</li>
  <li>treat numbers as triangle sides and compute hypotenuse by Pythagorean rule</li>
  <li>min or max of two</li>
  </ul>
  <p>Make sure students understood what every of these functions had done to two numbers. Ask them &quot;anything else?&quot; and if they produce more math examples (combinations), this is good, but at some step stop them and show this:</p>
  <ul>
  <li>drop second (i.e., function which takes two arguments, and returns only first)</li>
  <li>drop first</li>
  <li>return those two back as a 2-tuple</li>
  <li>return those two back as a swapped 2-tupple</li>
  <li>return nothing</li>
  </ul>
  <p>Now again, ask students &quot;anything else?&quot;, make sure they understand how a function can take two arguments and do nothing, and return nothing, this is quite counterfactual for some. It is good if some list results are guessed, otherwise, show yourself:</p>
  <ul>
  <li>return list, where first arg is repeated second arg times</li>
  <li>range of numbers from min arg to max arg</li>
  <li>sum of consecutive numbers from min arg to max arg</li>
  <li>numbers from 1 to first arg with second arg step</li>
  <li>numbers from 1 with first arg as step and second arg item count</li>
  <li>first arg count primes, each divided by second arg</li>
  </ul>
  <p>Make sure students are able to visualize each example, of how two numbers transform to list. The biggest sign of this is when they get the pattern and try to produce own examples. After some time, you break their brain again:</p>
  <ul>
  <li>a list where number 42 is repeated first arg times, and then second arg is repeated 200 times</li>
  <li>a list where first arg is repeated 3 times, then second arg is repeated 3 times, then first arg is repeated 4 times</li>
  <li>a list where 3 is repeated first arg times, then 0 and this whole pattern is repeated second arg times x5</li>
  </ul>
  <p>Ask students &quot;anything else?&quot;, and if they grok pattern they should start some random list generations with some random numbers. The more random examples, the better, creativity is not easy. But at some point you break their brain once more:</p>
  <ul>
  <li>given two args, return a function which will take one more arg and return sum of three args</li>
  <li>given two args, return a function which will take one arg and ignore previous two args alltogether, and return square of an argument</li>
  <li>given two args, return a function which takes list as an argument, and returns range of numbers from this list using two previous args</li>
  </ul>
  <p>It is mind-shifting to think about function which returns a function, but if students can provide own examples (however fantastic), this is very good sign. Good if somebody can create function which returns a function which returns a function. But if not:</p>
  <ul>
  <li>takes 2 args, returns a function, which takes no args and returns a function which takes 1 arg and returns pair of previous 2 args each multiplied by arg in innermost function (sic!)</li>
  <li>return a function, which takes a function as an argument, and returns application of this arg function to previous 2 args</li>
  <li>return a function, which takes a function as an argument, applies it first arg, and then applies itself to this result, and continues this process second arg times</li>
  </ul>
  <p>After students brain is completely devastated by trying to imagine this, you show a few more examples:</p>
  <ul>
  <li>compare two numbers and return &quot;less&quot;, &quot;greater&quot;, &quot;equal&quot;</li>
  <li>return True if both are greater than 42, False otherwise</li>
  <li>convert numbers to strings and return them combined</li>
  <li>translate these numbers to Mongolian and return a tuple of string representations</li>
  <li>... show creativity!</li>
  </ul>
  <p>Ok, and after all this done, ask the original question again &quot;what can a function do with two numbers&quot; and check student recall, how many of these examples were really grokked. The simple <code>def plus(a, b): return a + b</code> will now look different in student's head</p>
  <p>P.S. After lesson is done, you tell &quot;all the examples we talked about were about pure functions. But if we consider impure functions, then ...&quot; :)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-22T05:40:33.897Z
Answers.LastActivityDate: 2020-10-22T05:40:33.897Z
Answers.UserDisplayName: danbst
Answers.Body: <blockquote>
  <p>Can I craft an exercise ... that would give a dramatically wrong output, e.g. cause the screen to &quot;blow&quot; up if the function <strong>print</strong>-ed instead of <strong>return</strong>-ed...</p>
  </blockquote>
  <p>[my emphasis]</p>
  <p>Early in my Python classes I do (something like) this:</p>
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; def foo(x):
  ...   print(2*x + 3)
  ... 
  &gt;&gt;&gt; def bar(x):
  ...   return (2*x + 3)
  ... 
  &gt;&gt;&gt; foo(4)
  11
  &gt;&gt;&gt; bar(4)
  11
  </code></pre>
  <p>So they <strong>look</strong> quite the same, don't they?</p>
  <p>Now <strong>use</strong> them:</p>
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; [bar(x) for x in range(5)]
  [3, 5, 7, 9, 11]
  &gt;&gt;&gt; [foo(x) for x in range(5)]
  3
  5
  7
  9
  11
  [None, None, None, None, None]
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-23T05:17:00.903Z
Answers.LastActivityDate: 2020-11-01T13:31:59.340Z
Answers.LastEditDate: 2020-11-01T13:31:59.340Z
Answers.UserDisplayName: Rushi
Answers.Body: <p>Sorry this is a half answer because I haven't concrete examples right now. Besides, the answer with the electronic analogies is already so great. But one point attracts me.</p>
  
  <p>At the level where I imagine your students are, they do not need the modularity effectiveness that can be obtained with well structured functions. Indeed, you report from them mistakes of not following "your" way of doing things, but no concrete reason is dictating that your way is better than, for example, the cited way of implementing <code>add1</code>, therefore they may be lost wondering what are you asking them.</p>
  
  <p>The "problem", so to say, is that in their situation functions do not serve any purpose of encapsulation, reuse, modularization. More: these are most probably just empty words for them, they don't have any encapsulation need and are therefore unable to recognize this never encountered concept. So this is why the electronic analogy is great: it lets them connect with something they know.</p>
  
  <p>But from the computing side of things there's more. It seems that you are letting your students somehow follow a path of prehistoric programmers discovering how to take advantage of that strange creature called processor armed only with a sharp stone.</p>
  
  <p>We may think that all our reuse or modularization needs will be perfectly served by <code>goto</code>. One sunny morning a programmer wakes up in his cave and observes that some problems are very very similar to one another and can be solved by repeating the same basic instructions with slight differences. He takes his stones and starts forging his <code>goto</code> into something more elaborate: the point where you land after the jump doesn't start right away but inspects some mysterious values called parameters and only after that the work can begin, being different depending on the actual parameters seen.</p>
  
  <p>If this is the learning path that the students are following then what is needed is a case where they must do some operations in differing incarnations: if they do not feel this need they won't understand what are you wanting from them. In your introducing examples nothing of this kind was needed: if I just need to add1 then read, +1, write, end of story.</p>
  
  <p>One problem alone doesn't need functions. A class of similar problems does. Ultimately, a computing system is one calculating a function, i.e. solving a class of problems, represented as differing inputs.</p>
  
  <p>Now, unfortunately I don't have such examples in mind right now, but I hope it may be easier to invent them by having a clearer picture of their nature.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-14T13:07:42.807Z
Answers.LastActivityDate: 2019-03-14T13:07:42.807Z
Answers.UserDisplayName: user9137
Body: <p>I have a group of students with a very shaky understanding of functions and their purpose (encapsulation, reuse, modularization). The assignments I give them require them to write simple functions that return boolean T/F, an integer or a simple Python object (str, list, etc.), then have the caller use the returned value in a print statement. Their programs are written in Python and run from the Windows command line.
  The problem is that from the command line, the programs work but if you look inside it's a mess. The functions ignore the parameters to reach into the main for global variables (misunderstand the idea of parameter passing), the input validity checking is done in the caller (defeating the idea of encapsulation, e.g is day of month valid for a given month), or the function prints the result without returning anything. The worst that I get is the function does all the work of the main and of the function, for example when asking for a simple function to add 1 to its argument, I get:</p>
  
  <pre><code>def add1(x):
      myval = int(input("Number?"))
      print(myval+1)
  
  add1(x)
  </code></pre>
  
  <p>...which of course is nonsense.</p>
  
  <p>Here's my question... Can I craft an exercise that calls a simple function like add1() that would give a dramatically wrong output, e.g. cause the screen to "blow" up if the function printed instead of returned, or stalled if it kept asking for input. I'm thinking of something that reads a large file of text or numbers.</p>
  
  <p>The students are at the community college level and are not programmers -- they are in electronics technology (and some are convinced that there is no software involved in electronics). I use Python but this is (mostly) language independent (I see the same shaky understanding with C++ functions in the Arduino world in another course).</p>
  
  <p>Thanks in advance
   --Louis</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-28T01:10:39.410Z
Comments.Text: Something that reads from stdin and writes to stdout (I am hopelessly dating myself here) is a good exercise. Next thing you know, they'll be dealing with stream processing of big data.
Comments.UserDisplayName: Scott Rowe
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-28T19:11:21.160Z
Comments.Text: @ScottRowe Hmm, yes. In fact they will eventually be dealing with IoT concepts and aggregating data from multiple sensor "things".
Comments.UserDisplayName: Louis B.
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-28T21:57:09.093Z
Comments.Text: And you didn't even need to throw "the book" (Structure and Interpretation of Computer Programs) at them!
Comments.UserDisplayName: Scott Rowe
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-10-23T04:31:29.037Z
Comments.Text: If you want to make shell-script-y (a la @ScottRowe) 
   **PLUS** functional code you could use/assune a tool like google-fire https://github.com/google/python-fire/blob/master/docs/guide.md . (not that I recommend it!! Just sayin'...  )
Comments.UserDisplayName: Rushi
Tags: <best-practice><teaching-analogy><exercises>
Title: Exercise or example to reinforce idea of functions?
ThreadText: # Exercise or example to reinforce idea of functions?
  
  (Asked by: username_0 on 2018-10-26)
  
  I have a group of students with a very shaky understanding of functions and their purpose (encapsulation, reuse, modularization). The assignments I give them require them to write simple functions that return boolean T/F, an integer or a simple Python object (str, list, etc.), then have the caller use the returned value in a print statement. Their programs are written in Python and run from the Windows command line.
  The problem is that from the command line, the programs work but if you look inside it's a mess. The functions ignore the parameters to reach into the main for global variables (misunderstand the idea of parameter passing), the input validity checking is done in the caller (defeating the idea of encapsulation, e.g is day of month valid for a given month), or the function prints the result without returning anything. The worst that I get is the function does all the work of the main and of the function, for example when asking for a simple function to add 1 to its argument, I get:
  
  ```
  def add1(x):
      myval = int(input("Number?"))
      print(myval+1)
  
  add1(x)
  
  ```
  
  ...which of course is nonsense.
  
  Here's my question... Can I craft an exercise that calls a simple function like add1() that would give a dramatically wrong output, e.g. cause the screen to "blow" up if the function printed instead of returned, or stalled if it kept asking for input. I'm thinking of something that reads a large file of text or numbers.
  
  The students are at the community college level and are not programmers -- they are in electronics technology (and some are convinced that there is no software involved in electronics). I use Python but this is (mostly) language independent (I see the same shaky understanding with C++ functions in the Arduino world in another course).
  
  Thanks in advance
  --username_0
  
  **Tags:** best-practice, teaching-analogy, exercises
  
  > **Comments:**
  > **username_1**: Something that reads from stdin and writes to stdout (I am hopelessly dating myself here) is a good exercise. Next thing you know, they'll be dealing with stream processing of big data.  
  > **username_0**: @username_1 Hmm, yes. In fact they will eventually be dealing with IoT concepts and aggregating data from multiple sensor "things".  
  > **username_1**: And you didn't even need to throw "the book" (Structure and Interpretation of Computer Programs) at them!  
  > **username_2**: If you want to make shell-script-y (a la @username_1)   **PLUS** functional code you could use/assune a tool like google-fire https://github.com/google/python-fire/blob/master/docs/guide.md . (not that I recommend it!! Just sayin'...  )  
  
  ---
  
  ## 8 answers
  
  ### Answer by username_6 on 2018-10-27. Score: 7 | Accepted answer
  
  Instead of an example, use a metaphor. Get yourself a frisbee - ultimate disk and two packs of sticky notes. You are the caller of a function. Another person is the function itself. Write a value on the sticky note and stick it to the frisbee. The *pass* it to the function (person). The person catches the disk, and replaces the stick note with a new one with a new computed *function* value. They then *return* the disk to you. You get the computed value and announce it.
  
  Without the return value you get nothing. You can use any function you like but zero or one arguments is the easiest to manage.
  
  A slightly more sophisticated version can be used to discuss pass by value and pass by reference.
  
  Due to username_5 Clancy of UCBerkeley. It is a classic from many years ago.
  
  > **Comments:**
  > **username_0**: I've tried something like that but without the physical object. Scenario: we're both working late in the electronics lab and I forgot my calculator. I call out to you, Hey Fred, what's the square root of 4? You return the result of the computation, It's 2! I called the function by name, Fred, passed a value and waited for a returned value. The function didn't care what problem I was solving, and I the caller didn't care how the answer was computed.  
  > **username_7**: That's a great suggestion @username_6!  Thanks for sharing that answer!  
  
  ---
  
  ### Answer by username_1 on 2018-10-27. Score: 7
  
  I saw the light at the end of the fiber optic cable as soon as you said that these are students of electronics. There is nothing more modular and encapsulated than that! Software is simply electronics without the hardware part.
  
  If you said to them, "What are the building blocks of a simple AM radio receiver?" they would (hopefully) reply something like: antenna, tuning, rectification, filter, audio output... A crystal radio has all of those. They already know that it is pointless to build the antenna in to the filter, or duplicate the tuning parts more than once. They know that they can work in teams to produce parts that fit together to make a complete radio.
  
  So speak to them in a language they already know: the functional relationship of parts and wholes. Using globals instead of parameters would be like having extra cables going outside the radio to connect different parts of it to the signal path or the power supply. Can you say "Rube Goldberg"? I knew you could.
  
  Man, let me teach them. I have been studying electronics on my own since before anyone I knew even had seen a computer. Electronics students should be the easiest audience in the world to teach programming to. Use examples they already know.
  
  > **Comments:**
  > **username_1**: I am sure to draw fire from the community by talking about *Software* in terms of real-world things, instead of explaining it as a closed, hermetic universe entirely consisting of concepts unrelated to anything the students have encountered before.  
  > **username_0**: Bingo! And no, you won't get flak from me for mixing electronics and software. I deal in embedded systems and a system diagram can be any of pure hardware, pure software or a mix (e.g. state machine, FPGA).  
  
  ---
  
  ### Answer by username_8 on 2020-10-22. Score: 3
  
  I like to think about pure function as a transmutation of inputs. Just like in Alchemy you connect Fire and Water to get Steam. Fire and Water are inputs, Steam is output. Or treat function as an assembly machine -- it takes resources, and converts those into another resource.
  
  After that try posing a question: given two integer numbers as an input, what kinds of transmutation about these two numbers can you think about?
  
  So first idea should be:
  
  * add numbers to get a number
  * multiply numbers
  * substract numbers
  * divide numbers
  
  And usually that's it, I mean, student fantasy often stops here. Now your turn to make "how did I not think about it?" in student's brain:
  
  * substract first number from second (change argument order)
  * modulo divide two numbers
  * integer part of division
  * exponentiate numbers
  * treat numbers as triangle sides and compute hypotenuse by Pythagorean rule
  * min or max of two
  
  Make sure students understood what every of these functions had done to two numbers. Ask them "anything else?" and if they produce more math examples (combinations), this is good, but at some step stop them and show this:
  
  * drop second (i.e., function which takes two arguments, and returns only first)
  * drop first
  * return those two back as a 2-tuple
  * return those two back as a swapped 2-tupple
  * return nothing
  
  Now again, ask students "anything else?", make sure they understand how a function can take two arguments and do nothing, and return nothing, this is quite counterfactual for some. It is good if some list results are guessed, otherwise, show yourself:
  
  * return list, where first arg is repeated second arg times
  * range of numbers from min arg to max arg
  * sum of consecutive numbers from min arg to max arg
  * numbers from 1 to first arg with second arg step
  * numbers from 1 with first arg as step and second arg item count
  * first arg count primes, each divided by second arg
  
  Make sure students are able to visualize each example, of how two numbers transform to list. The biggest sign of this is when they get the pattern and try to produce own examples. After some time, you break their brain again:
  
  * a list where number 42 is repeated first arg times, and then second arg is repeated 200 times
  * a list where first arg is repeated 3 times, then second arg is repeated 3 times, then first arg is repeated 4 times
  * a list where 3 is repeated first arg times, then 0 and this whole pattern is repeated second arg times x5
  
  Ask students "anything else?", and if they grok pattern they should start some random list generations with some random numbers. The more random examples, the better, creativity is not easy. But at some point you break their brain once more:
  
  * given two args, return a function which will take one more arg and return sum of three args
  * given two args, return a function which will take one arg and ignore previous two args alltogether, and return square of an argument
  * given two args, return a function which takes list as an argument, and returns range of numbers from this list using two previous args
  
  It is mind-shifting to think about function which returns a function, but if students can provide own examples (however fantastic), this is very good sign. Good if somebody can create function which returns a function which returns a function. But if not:
  
  * takes 2 args, returns a function, which takes no args and returns a function which takes 1 arg and returns pair of previous 2 args each multiplied by arg in innermost function (sic!)
  * return a function, which takes a function as an argument, and returns application of this arg function to previous 2 args
  * return a function, which takes a function as an argument, applies it first arg, and then applies itself to this result, and continues this process second arg times
  
  After students brain is completely devastated by trying to imagine this, you show a few more examples:
  
  * compare two numbers and return "less", "greater", "equal"
  * return True if both are greater than 42, False otherwise
  * convert numbers to strings and return them combined
  * translate these numbers to Mongolian and return a tuple of string representations
  * ... show creativity!
  
  Ok, and after all this done, ask the original question again "what can a function do with two numbers" and check student recall, how many of these examples were really grokked. The simple `def plus(a, b): return a + b` will now look different in student's head
  
  P.S. After lesson is done, you tell "all the examples we talked about were about pure functions. But if we consider impure functions, then ..." :)
  
  
  ---
  
  ### Answer by username_2 on 2020-10-23. Score: 3
  
  > Can I craft an exercise ... that would give a dramatically wrong output, e.g. cause the screen to "blow" up if the function **print**-ed instead of **return**-ed...
  
  [my emphasis]
  
  Early in my Python classes I do (something like) this:
  
  ```py
  >>> def foo(x):
  ...   print(2*x + 3)
  ... 
  >>> def bar(x):
  ...   return (2*x + 3)
  ... 
  >>> foo(4)
  11
  >>> bar(4)
  11
  
  ```
  
  So they **look** quite the same, don't they?
  
  Now **use** them:
  
  ```py
  >>> [bar(x) for x in range(5)]
  [3, 5, 7, 9, 11]
  >>> [foo(x) for x in range(5)]
  3
  5
  7
  9
  11
  [None, None, None, None, None]
  
  ```
  
  
  ---
  
  ### Answer by username_9 on 2019-03-14. Score: 2
  
  Sorry this is a half answer because I haven't concrete examples right now. Besides, the answer with the electronic analogies is already so great. But one point attracts me.
  
  At the level where I imagine your students are, they do not need the modularity effectiveness that can be obtained with well structured functions. Indeed, you report from them mistakes of not following "your" way of doing things, but no concrete reason is dictating that your way is better than, for example, the cited way of implementing `add1`, therefore they may be lost wondering what are you asking them.
  
  The "problem", so to say, is that in their situation functions do not serve any purpose of encapsulation, reuse, modularization. More: these are most probably just empty words for them, they don't have any encapsulation need and are therefore unable to recognize this never encountered concept. So this is why the electronic analogy is great: it lets them connect with something they know.
  
  But from the computing side of things there's more. It seems that you are letting your students somehow follow a path of prehistoric programmers discovering how to take advantage of that strange creature called processor armed only with a sharp stone.
  
  We may think that all our reuse or modularization needs will be perfectly served by `goto`. One sunny morning a programmer wakes up in his cave and observes that some problems are very very similar to one another and can be solved by repeating the same basic instructions with slight differences. He takes his stones and starts forging his `goto` into something more elaborate: the point where you land after the jump doesn't start right away but inspects some mysterious values called parameters and only after that the work can begin, being different depending on the actual parameters seen.
  
  If this is the learning path that the students are following then what is needed is a case where they must do some operations in differing incarnations: if they do not feel this need they won't understand what are you wanting from them. In your introducing examples nothing of this kind was needed: if I just need to add1 then read, +1, write, end of story.
  
  One problem alone doesn't need functions. A class of similar problems does. Ultimately, a computing system is one calculating a function, i.e. solving a class of problems, represented as differing inputs.
  
  Now, unfortunately I don't have such examples in mind right now, but I hope it may be easier to invent them by having a clearer picture of their nature.
  
  
  ---
  
  ### Answer by username_3 on 2018-11-19. Score: 1
  
  I think the example you showed on the original question is more about exception handling and unit testing rather than explaining the concept of function.
  
  You may want to teach students on explaining the concept of function in a very simple way such as "Why is a lego so successful? Because it uses blocks! Function is like a lego block. It's about reusability, modular programming.", and give them enough time (maybe several weeks or so) to practice with different kind of examples to naturally get familiar with using functions.
  
  The error-handling and unit-testing are more advanced concept and technique. It's probably best to teach these concepts once students are pretty good at writing code in general. I would suggest to actually teach this almost at the end of the course. It's important in the real-world software development, but for learning, I think these are less important compared to other topics.
  
  
  ---
  
  ### Answer by username_4 on 2018-10-26. Score: 0
  
  You can call the function using the [python unit test framework](https://docs.python.org/3/library/unittest.html) and run different tests against it. Simple print-statements and other things will most probably no longer work.
  
  > **Comments:**
  > **username_0**: Exactly, I use pytest though, it's simpler. pytest errors out if there is unexpected output when importing the module. The  problem is not in detecting their mistakes, those are pretty obvious, the real problem is that they persistently don't get it!  
  
  ---
  
  ### Answer by username_5 on 2018-11-06. Score: 0
  
  If classes are guns, functions are sort of like bullets. Not a guns take the same rounds, not all classes have the same functions. But some classes can have the same base classes therefore have some similar functions; some guns have the same manufacturer and take the same bullets. Instances of a class can call functions, just as a gun can shoot bullets. Maybe thats why call of duty calls it "create a class"...
  
  
  ---
  
  