Bundle it, give it a name
Give the steps of making ramen the name "make ramen," and next time that one phrase is enough. A function bundles several things into one chunk and gives it a name. Just call the name and it all happens.
Bundle it and name it
Boil water, add noodles,
add the soup, serve in a bowl.
Bundle these four steps
into one chunk called
"make ramen."
Then give it a name.
This bundle is a function.
Bundle it below.
Scattered steps → one named chunk. (Four steps bundled as "make ramen")
Bundling and naming
clears your head.
Without saying "boil water, add noodles…"
one by one,
the phrase "make ramen"
gets it across.
You hide the complexity
behind one name.
Call it and it all happens
Since you've named it,
now you just call the name.
Say "make ramen,"
and the four bundled steps
all happen in order.
No need to look inside
again.
The name stands in for the work.
Call the name, and the bundled steps run in order. ("make ramen" → four steps in order)
Why this is handy
is you can forget the "how."
Know the name and "what it does,"
and whatever's inside,
you just call it.
You tuck the complexity
behind the name.
Put in ingredients, get a result
A function can take ingredients.
Put 5 into a function called "double,"
and out comes 10,
put 7 and out comes 14.
Put something in (input),
it works inside
and gives back a result (output).
Like a little machine.
Input → (function) → output. (double: 5 → 10 · changes with what you put in)
Even with the same name,
put in something different
and out comes something different.
Put 3 into "double" for 6,
100 for 200.
Just change the ingredient
to do the same work.
Reuse the function you made
The real good of a function
is you can reuse it.
Make "double" once,
and whenever you need it,
just call the name.
For 5, for 8, for 12.
Make it once,
use it many times.
Call the "double" you made once, many times. (5→10 · 8→16 · 12→24)
In lesson 7,
"write once, do many," right?
A function is similar.
Make it well once,
and across the whole program
you keep pulling it out.
One good function
saves work for good.
A way to tame complexity
A function is
a way to tame complexity.
Bundle several things,
give it a name,
pull it out again.
Even a big program
really runs by functions
calling one another.
Once it's bundled and named, now. (A function = bundle, name, reuse → collections · big programs · beyond)
Well done getting this far.
Named values, knew their kinds,
judged, calculated,
repeated, and now bundled and named.
You've got
almost all the basic tools
for building a program.