How would I introduce someone to the idea of functions?
When you want a room painted, you call a painter.
When you want a sink unclogged, you call a plumber.
When you want something done, you call someone who can do it.
Let's create a word that means "someone you can call to do something".
Painters and plumbers are examples of "callables". They are someone you can call and they specialize in doing the thing you called them for.
In programming, there's something very similar. There are things you can "call" to perform some work. Instead of painting and plumbing, you can call something to "display a message to the user" or another thing to "calculate the square root".
You'll quickly learn at least one other word that these "callable" things go by. The most common is probably "function". You'll probably hear phrases like "call the
Terminology, like "function", "method", and "procedure" are eventually important. But what's more important is understanding the concept.
The concept is: the idea of a "callable" thing, like a painter or a plumber, (or for programming examples, a printer or a square-rooter).
How do you call something?
In the real world, you might call a plumber with your cell phone, you might use a text message, or if your next door neighbor is a plumber, you might yell over the fence.
In programming, you call something by typing certain characters. Many common programming languages use parentheses as a way of saying "call this thing".
print(...) - call the printer
reverse(...) - call the reverser
format(...) - call the formatter
But those are just examples. Other programming languages might have use different characters, or a different ordering of the characters.
(print ...) - call the printer
What is the
... inside the parentheses?
That's the message you want to give to the "callable".
The printer will obviously want to know "What do you want me to print?"
print("Hello, world!") - call the printer, tell it to print "Hello, world!".
Or, using a different programming language, maybe it would be something like:
(reverse [1, 2, 3]) - call the reverser, tell it to reverse the list
[1, 2, 3].
Many beginning programming texts refer to these things as functions. Why am I using "callable"?
Let's dig in to another type of callable, a "method".
Imagine YOU are a callable; an "opener". You're job is to open things.
For example, if someone said:
open(door) you would open the door
open(letter) you would open the letter
open(jar) you would open the jar.
open(account) would open a bank account.
Now, obviously there is a big difference between opening a door and opening a jar and opening a bank account. The type of work that needs to be done is completely different, and there could be an infinite number of things that require different types of work to "open".
But the "concept" behind the work is the same: "open".
How can a single callable, an "opener", handle so many different things?
It might be doable for a human, like yourself. But every tiny detail of every single step needs to be spelled out explicitly for a computer. It's difficult.
Instead of having one universal "opener", what if there were a bunch of specialized openers?
door_opener(door) would open a door
letter_opener(letter) would open a letter
That would be one way of handling things. But it's kind of redundant. If I know I'm dealing with a door, why can't I just say "open the door"? Why do I have to say "open the door with the door opener"?
Well, what if the thing was it's own "opener". What if you could tell the door to open itself?
Instead of having one universal opener that must know how to open everything, each thing that can be "opened" has it's own personal "opener".
In the real world, how would you call on a door to open itself? You wouldn't use a phone the same way you would call a plumber. You might press a button or, if you have a smart-home, you might talk to your Alexa.
We also need a different way to tell our computer programs that we want to use a specialized "opener" (or other "doer of things") that is specific to a particular object like a door or a letter.
. is commonly used as the way of telling a program to use a specialized "callable". It's a "callable" where the work is done by the object itself. Instead of having an "opener" you have a "door" that knows how to "open" itself, and a letter that knows how to "open" itself.
The major value of this concept may not be apparent until later. But the concept is simple enough to understand now.
The term for this concept is a "method".
A function is a "callable". In many programming languages, the "call" is made with parentheses, like
A method is a "callable" that knows how to do work for a specific type of object, like a door, or a jar, or a letter from the mail (or in computer-talk, a character, a number, or a list), like