seegongsik
Programming

What is a function in programming

Give the steps of making instant noodles the name "make noodles," and next time that one phrase is enough. A function bundles several tasks into one chunk and gives it a name. Call the name, and all of it happens.

01

Bundle it and name it

Boil the water, add the noodles, add the soup base, pour it into a bowl.
Bundle these four steps into one chunk called "make noodles" and give it a name.
That bundle is a function.
Below, you can watch scattered steps come together into one chunk.

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

Scattered steps → one named chunk. Four steps are bundled into "make noodles."

Bundling and naming makes things tidy in your head.
You don't have to say "boil the water, add the noodles..." one by one; "make noodles" says it all.
It hides something complicated behind a single name.

02

Call it and it all happens

Now that it has a name, you just call the name.
Say "make noodles," and the four steps bundled inside all happen in order.
There's no need to look inside again; the name does the job.

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 noodles" → four steps, one at a time.

The reason this is handy is that you can forget the "how."
As long as you know the name and what it does, you can just call it, whatever is inside.
The complexity stays hidden behind the name.

03

Put in ingredients, get a result

You can put ingredients into a function.
Put 5 into a function called "double" and you get 10; put in 7 and you get 14.
You put something in (input), it works inside, and it gives back a result (output), like a small 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. The result depends on what you put in.

Same name, but put in something different and something different comes out.
Put 3 into "double" and you get 6; put in 100 and you get 200.
You change only the ingredients and have the same job done.

04

Use a function again

The really great thing about functions is that you can reuse them.
Once you've made "double," you just call its name whenever you need it: for 5, for 8, for 12.
Make it once, use it many times.

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

Call "double," made once, again and again. 5 → 10 · 8 → 16 · 12 → 24.

The loops in Lesson 7 were also "write once, use many times."
Functions are similar: build one well, and you can keep pulling it out all over your program.
One good function saves work again and again.

05

A way to tame complexity

Functions are a way to tame complexity.
Bundle tasks, give them names, and use them again.
Even big programs really run 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
In the next lesson we'll meet "recursion," where a function calls itself.

Now that you can bundle and name, what's next. Function = bundle, name, reuse → collections · big programs · beyond.

You've worked hard to get here.
Naming values, knowing their types, deciding, calculating, repeating, and now bundling things under a name.
You now hold nearly all the basic tools for building a program.

In one lineA function bundles several tasks into one chunk and gives it a name. Call the name and it all happens; put in ingredients and a result comes out; build it well once and you can reuse it all over your program. Functions hide complexity behind a name, and that's how big programs stay under control.
← Programming
Was this helpful? Support seegongsik