Teaching Functions, Part 2

Fabio Vinicius Binder
Teaching Programming Ideas
2 min readApr 10, 2019

Disclaimer: this text contains spoilers of the movie Dr. Strange.

The next step on teaching functions is to teach how to create and use your own functions. In my Swift classes I teach functions before conditionals and loops, so the question is: what kind of functions a student may create with no knowledge about the fundamental concepts of programming?

One possible answer is: tell a story! How? Two aspects of functions may be adressed by telling a story: organization and reuse (avoid repetition). The first one is easy, since stories have parts or chapters. And there are some good stories with parts that are repeated, so there you are, a story can be used as the starting point of understanding functions.

What about the end of Dr. Strange's movie? Several events are repeated: the death and a few phrases like "Dormammu", "I came to bargain", etc. We can create a function for each of these events, each function representing a part of the story that can be called to tell the story:

Each event of the story can be created as a simple function that prints a text

All those calls can be put together in a new function to perform the entire scene:

A scene from Dr. Strange's movie

And of course, it may be called more than once like the movie:

As in the movie, our scene is called more than once.

Resulting in the following output to the console:

The final result of our little story.

At the end of the class the students can be challenged to create their own stories. In my classes I gave them a suggestion: tell a story about a day in the life of a Hobbit. This theme has the benefit of having several repetitions, since Hobbits eat at least 7 times a day: breakfast, second breakfast, elevenses, lunch, afternoon tea, dinner, supper.

A story as a function may be used in the next programming classes: the student can insert elements with ifs, loops, arrays and any other topic of the course's syllabus.

--

--