9
$\begingroup$

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 --Louis

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Scott Rowe Oct 28 '18 at 1:10
  • $\begingroup$ @ScottRowe Hmm, yes. In fact they will eventually be dealing with IoT concepts and aggregating data from multiple sensor "things". $\endgroup$ – Louis B. Oct 28 '18 at 19:11
  • $\begingroup$ And you didn't even need to throw "the book" (Structure and Interpretation of Computer Programs) at them! $\endgroup$ – Scott Rowe Oct 28 '18 at 21:57
5
$\begingroup$

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 Mike Clancy of UCBerkeley. It is a classic from many years ago.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Louis B. Oct 27 '18 at 1:13
  • $\begingroup$ That's a great suggestion @Buffy! Thanks for sharing that answer! $\endgroup$ – Onorio Catenacci Mar 18 at 16:20
6
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Scott Rowe Oct 27 '18 at 14:39
  • 1
    $\begingroup$ 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). $\endgroup$ – Louis B. Oct 28 '18 at 18:59
1
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

You can call the function using the python unit test framework and run different tests against it. Simple print-statements and other things will most probably no longer work.

$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – Louis B. Oct 27 '18 at 1:18
0
$\begingroup$

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"...

$\endgroup$
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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