Functions?! Why do I need to know this? – HS Intro to Python Class at End of 2021-2022 School Year

TL;DR, Summary, Breakdown

  • Function use is equivalent to using names for things, actions, people, etc.
  • Reuse of functions is secondary to the simplicity provided by naming things
  • This is a superpower

We were winding down the Introduction to Python lecture. There were three of us leading the class, Teacher O, Tutor E, and myself. I asked, “Does anyone have any questions?”

Student X asked, “Why do we need functions?”

I was taken aback. I paused. It clearly is an abstraction concept, related to the idea of naming things. That’s a hard sell. I said, “It simplifies your code, but I’ll try to think of a better answer.”

Teacher O interjected, “It allows you to reuse code. Reusability is very important for the real world.”

I struggled, stammered, and committed to coming back with a better answer. I’m still struggling.

I explored the issue later with Student E. E was having trouble with their creative task. E wanted to create three space men, from their favorite game, “Among Us.” We worked to get E’s task running, but then came to the end of the class period, still needing to fulfill one element of the rubric: use a function with a return value.

When we came back next class period, E, made a telling mistake, putting “()” characters after a variable. They ran the code and an error was instantly visible. They didn’t read the error, just removing the “(),” and suddenly it ran again.

“Why did you do that?”

“I don’t know.”

“Do you know what those parenthesis mean?”

“I’m not sure.” [1]

I thought for a moment. “Can you put in your own words, the difference between a function and a variable?”

“I’m not sure.”

—I’ve commented on 1:1 interaction, and this is where it is incredibly useful. In a group setting, there are all sorts of pressures that are difficult to relieve. When I was a student, I felt pressure to show my knowledge or, maybe worse, to avoid looking ignorant. So, I wouldn’t speak. 1:1, though, we can use the teacher/student trust to draw the student out. I am able to gauge and even reconsider different directions. I’m able to tell stories about how I found the particular topic challenging when I was a student.

I tried a new tack, “Think of functions like verbs and variables like nouns. Is crewmate a verb or a noun?”

“A noun?” E asked, somewhere between confused and questioning.

I dug in farther. After a few moments of discussion, E asked, “what’s the difference between a verb and noun again?” We googled noun and verb. Thing and Action. Still, E struggled.

“Is createCrewmate a noun or verb?”

“No, a verb?” Correct, but the question hung in the air.

—Can I simplify this?

“Let’s stop talking about verbs and nouns. Let’s just talk about functions as actions and variables as things. Is crewmate an action or a thing?”

“A thing.” A clear statement. E didn’t waver.

“And is createCrewmate an action or a thing?”

“Action.” Another clear statement. E had this!

We were there. Functions were actions and variables were things. However, that’s only the beginning of the journey. Helping E to understand when to create a new function/action is a battle for another day. Honestly, I’ve worked with engineers with 15 years of experience who struggle with when to create a function.[2][3] Again, with the 1:1 time, I was able to actually get to the heart of what was wrong. It became an indicator, a canary, pointing to a problem:

My team and I need to explain functions better.

Here’s how Codecademy talks about functions in their Python 2/3 Course(s): https://www.codecademy.com/courses/learn-python/lessons/python-functions/exercises/what-good-are-functions

What Good are Functions?

You might have considered the situation where you would like to reuse a piece of code, just with a few different values. Instead of rewriting the whole code, it’s much cleaner to define a function, which can then be used repeatedly.

Here’s how Code.org explains functions in Minecraft Hour of Code (https://studio.code.org/s/coursee-2021/lessons/12/levels/10)

“A function is a specific set of instructions to accomplish a certain task.”

CS Academy Talks About Functions in their CS1 Course (https://academy.cs.cmu.edu/notes/1178)

A piece of code that can take certain inputs to perform certain actions

These all make sense to me, but when I put myself into the shoes of a 12 year old, a 15 year old, a 17 year old, who does not know that this is a superpower, these does not connect.

I’m still trying to figure out how to explain how we use abstraction everyday, succinctly and universally. We describe the process of running as just, “running,” even though it can be broken down and then down again into smaller steps or into sprinting and jogging and distance running and racewalking. Abstraction is so built-in to our thinking that we don’t even notice it.

It’s like the David Foster Wallace essay/speech/whatever, where two fish are swimming and the first asks “how’s the water, today?” The other one replies, “what the hell is water?” [4]

Describing why we use functions can also be challenging, but to pile on the metaphors, if you can see, you have no idea why glasses are so important. If you are colorblind, you have no idea what it is to see more colors. If you are not colorblind, you have no idea what it’s like to be colorblind. Thinking about the examples from Code.org, CS Academy, and Codecademy, they are trying to explain colors to someone who can’t see them. I think our curriculums are missing the mark for the students who aren’t already interested in computer science. Again, THE BEST TEACHERS PUT THE SUBJECT INTO CONTEXT.

Steve McConnell wrote in his wonderful book, Code Complete, that the fundamental problem of software is managing complexity. Take a moment. Let that soak in. We are managing complexity. One of the fundamental capabilities of abstraction in language and how our brains interact with it, whether English, Spanish, Mandarin, or Python, is helping us manage this complexity and communicate with others.

I’m not ready to create a single, universal statement that works for all students. I will give a short example that should be very adaptable to students in a 1:1 or even group setting [4]:

“What do you like to do? What are you interested in,” I ask.

She responds, “I like to run.”

“And what is running?”

“What do you mean?”

“Can you describe to me the process of running? What do you do with your feet? How is it different than walking?” I’m careful to put it in a curious tone. I’m trying to invite an open response.

“Well, I guess, I take one foot and put it in front of the other. Then I take the other foot, and put it in front. I do this fast. It’s faster than walking. I think it’s also when one of your feet isn’t touching the ground.”

“Great. Now why do you say you run instead of, let’s go out and put one foot in front of the other and repeat that really fast? Why do you just say, let’s go running.”

The student is dumbfounded. I pause to allow her to think.

“Is it easier to say that? Is it natural? Is it simpler?” I say this slowly, calmly. I lead a little this time with some options to avoid any sense of a trap. The more we can do this, the more sense of trust we will build and the more I can leave questions open ended.

“Yeah.”

“So, I know you like to post on Instagram. Can you describe how to post to me?”

“I take a picture and post it.”

“Do you need to open Instagram every time?”

“Yeah, I guess so.”

“Do you ever add any filters or captions?”

“Yes.”

“Why do you say, ‘I post’ instead of saying, ‘I’m going to open Instagram, tap new post, take a picture, add a filter, add a caption, and then tap new post?’”

“Because it’s easier?”

“That sounds like a question. Are you sure?”

“…, … Yeah.”

“Right. And you can talk to anyone who uses instagram or runs and they will pretty much know what you mean. When I think about it, it’s really the process of naming complex things for simplicity. It really helps out thinking about it more simply, and it also helps you communicate more easily with others.”

Some final thoughts on reuse.

In my humble opinion, reuse is not a a primary explanation and actually obscures the real superpower here — simplification. Example: what about using a function only once? Wrapping a complex set of steps or ideas up into another named idea is fundamental to simplification and is very useful for single use. Second, copying code is how many people reuse code, even professionals. I’m not even sure there’s a difference between copying a function name and copying the code inside it. The difference is in the complexity the name of the function manages for us when we read the code—the shorthand of saying “run” or saying “post to Instagram”.

We will need to repeat this exercise. We will need to repeat it across subjects. This is at the heart of computational thinking. It’s the same in English as in Spanish, Mandarin, and Python. Oh, and math. [4]

  • So, what’s the best response you’ve ever heard to the question, “why do we need to know about functions?”
  • Is reuse is more important than the simplification of naming?
  • Is this process the same in math, writing, and computer science?
  • What am I missing?  What’s the critical piece that unifies all of this or… totally invalidates my thesis? [5]

[1] This was the point where it became clear to me, E did not have mastery of functions.

[2] One of my first professional projects was taking a research programmer’s code which had up to 15 nested blocks in a single C++ function. It was actually a great learning experience, because I realized I needed to decipher each line and turn it into a well named function to understand the program.

[3] I remember once, as as manager, digging into a developer’s code as our timeline slipped. Imagine my horror when I found a 1000 line function with 50+ complex conditionals (if/else/else if) and all sorts of interdependencies and complex states. Of course, there were also no automated tests.

[4] (I think. I don’t know Mandarin, and am less than fluent in Spanish and Python. I am fluent in thinking though.)

Special thanks to my new friend, David Nassar, who in talking about explaining functions told how he used a very complex act of getting a glass of water, being simplified as “please get me a glass of water.” This example connected with my experiences above and further supports the conviction of my belief that great teachers put things into context for students

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: