← Return to topic

I can't get FUNCTIONS to click!

Jayzhy · 7 Sep 2026 at 02:43 · permalink

I can not for the life of me understand how functions work. Anytime I try to learn how to create a function, my brain literally crashes into itself and I turn into Patrick when he stars drooling and goes all slack-faced.

Does anybody have any analogies that had that "AH HA!" moment?

Thanks!

Blackcore98 · 7 Sep 2026 at 02:45 · permalink

For me it clicked when I stopped thinking of a function as a "thing" and started thinking of it as a recipe: you hand it ingredients, it follows steps, and it hands you back a dish — if you forget the ingredients or steps, you get a burnt casserole, not a crash. Once I saw it as just naming a block of steps so I can reuse it later, the magic wore off.

smigolek86 · 7 Sep 2026 at 02:46 · permalink

I just think of it as basically the same as with math. I’m assuming you’ve taken at least some level of algebra, no? Think f(x), that’s a function.

Apenas_jc · 7 Sep 2026 at 02:48 · permalink

Functions are just a way of grouping code that you might want to re-use later with different numbers. Think of it like a rubber stamp. Instead of re-drawing an icon every time, you can just use the stamp. Except, you can change the ink from blue to red, and so on.

bahbahblacksheep9072 · 7 Sep 2026 at 02:49 · permalink

I think of functions as a box of "magic". You put a bunch of stuff into it, the box completes a bunch of stuff, and then it spits out something new.

itselRuben17 · 7 Sep 2026 at 02:49 · permalink

A function is just reusable code, say for example:

bool is_adult(int age) {

return age > 18;

}

OK, so say you're making a program that needs to check if a user is an adult, you call this function everywhere like:

if (is_adult(23)) {

//Allow access to loads of porn
}

This makes code easier to think about and also easier to fix because my "is\_adult" function is wrong.

It should be "return age >= 0" not just >, because that means you need to be 19 to be an adult, not 18.

Most functions are a bit more complicated than that, but basically it's so you can split up and re-use code so you're not constantly repeating yourself.

pathselector · 7 Sep 2026 at 02:49 · permalink

Just curious OP , what programming language you are learning ?

m1k3ol · 7 Sep 2026 at 02:50 · permalink

They are like recipes.

function add (x: number, y: number): number // This takes two numbers and returns a number
function repeat(x: number, word: string): string // Takes a number and a string and returns a string

Whenever you have multiple lines that need to stay together, consider putting them in a function and give that a good name.

slunk432 · 7 Sep 2026 at 02:52 · permalink

Imagine a function that puts two things together that's all it does so you give it parameter1 skibbidy and parameter2 toilet and it returns skibbidytoilet. Now if you provide parameter1 Six and parameter2 Seven what will your function return?

ArmanK97 · 7 Sep 2026 at 02:58 · permalink

I would also think functions as a stack frame in memory and once it’s returned the frame will be popped off from the stack. This imagery will come handy when you start looking into recursions

Dakota_Makoshi · 7 Sep 2026 at 02:58 · permalink

Functions are just a block of code that take X number (can be 0) inputs and perform some logic, and optionally return output

NIFORPLAY · 7 Sep 2026 at 03:01 · permalink

In integers age > 18 and age >= 19 are logically equivalent.

Lotoscar3456 · 7 Sep 2026 at 03:01 · permalink

Have you ever taken notes on how to do various things? You could have a little notepad and label one page, “How to fix a flat” Another could be “How to bake a cake” A third could be “How to set an alarm on your alarm clock”. Or how to compute the average of a list of numbers. Then when you want to do one of those things you pull out the right note and follow the steps. That’s all functions are: a list of steps to follow to accomplish something. Then if you are telling someone how to do one of those things instead of telling them all those steps, just hand them the note and say, “Do this.” And maybe (like computing the average) “Tell me what answer you got” You can think about each of those notes as a function. There’s really not a whole to them.