seegongsik
Saved words
Programming

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.

01

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.

Boil water
Add noodles
Add the soup
Serve in a bowl
The four steps are scattered apart.

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.

02

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.

make ramen
Boil water
Add noodles
Add the soup
Serve in a bowl
Call "make ramen" and the four steps inside run in order.

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.

03

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.

double
input × 2
10
Even the same function changes its output by what goes in. (5 → 10, 7 → 14)

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.

04

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.

5double10
8double16
12double24
The same function was called three times. Making it takes just once.

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.

05

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.

A function = bundle, name, reuse
Call the name and it happens · put in ingredients and a result · make once, use many.
Collections
Hold many in one
Big programs
Functions calling each other
Beyond
Into a deeper world
Next time we will touch "collections," holding many values together.

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.

In one lineA function bundles several things into one chunk and gives it a name. Call the name and it all happens; put in ingredients and a result comes out. Make it well once and you reuse it across the whole program. A function hides complexity behind a name, a way to tame a big program.
Was this helpful? Support seegongsik
Programming