← Return to topic

If functions can have other functions in them, why not use them instead of classes?

TheMightyElephant · 7 Sep 2026 at 16:01 · permalink

I´m a beginner in programming, and I´ve been wondering now why classes are so much more useful than functions. I know classes are good, but I don´t see why they are better than just having multiple small functions in a bigger function.

maltonik · 7 Sep 2026 at 16:06 · permalink

Global variables... Encapsulation... OOP... etc.

Qiong123 · 7 Sep 2026 at 16:06 · permalink

There's a saying about this: "objects are a poor man's closures, and closures are a poor man's objects": https://stackoverflow.com/questions/2497801/closures-are-poor-mans-objects-and-vice-versa-what-does-this-mean

AuthorSei-chan · 7 Sep 2026 at 16:06 · permalink

Classes also have data fields that goes along with those functions.

Braddock512 · 7 Sep 2026 at 16:08 · permalink

Because, before anything, a class is supposed to represent a state. You can't achieve that with functions within a function, once the function returns all variables are lost.

It is perfectly possible to code without classes, but that's just giving away a very useful tool.

draco1x1 · 7 Sep 2026 at 16:08 · permalink

you know, all the stuff m'kay

fructe · 7 Sep 2026 at 16:08 · permalink

Re-usability, decoupling, and structure. What you're describing quickly leads to the infamous "spaghetti code".

sega0719 · 7 Sep 2026 at 16:08 · permalink

Classes are better because they provide an easy way to keep track of object _state_, i.e. variables.

That would be harder to do if you just had a bunch of functions.

kubNfYN · 7 Sep 2026 at 16:09 · permalink

my surface take-away: either case, I'm a poor man

zabak31 · 7 Sep 2026 at 16:10 · permalink

Objects (made with classes) let you sort data better. Imagine a list of people and data associated with them, without a class creating an object type you would have to use arrays.

Using classes as functions will slow down your code, but for some projects you don't really need that much speed. Classes are useful for some things and should be avoided for others. Classes are part of a complete breakfast.

Feran · 7 Sep 2026 at 16:12 · permalink

Nah, a closure has state. It's just different ways of implementing the same idea.

But classes are a straightforward way of declaring that you want to look at your program data in terms of clumps of related state stored together. Usually each clump corresponds to some named entity in the business logic.

Havrik3434 · 7 Sep 2026 at 16:12 · permalink

Well, you are able to have all of that with functions/functional approaches, too.

kerentase · 7 Sep 2026 at 16:13 · permalink

So, you use functions to package up functionality, usually a behaviour (side effects) or just a value that depends on inputs.

Classes you would generally think of them as a type, like something you can create. It will then carry its context (constructor params) everywhere it goes. You would add methods to that class so you can use that context again and again without passing them to that function. This specific use case is basically function currying or partial application of functions, but you are free to reuse that context in more than one way.

Still on seeing classes as types, you can then instead of passing 6 numbers that correspond to 2 points in 3d space and having to call them x1, y1, z1, x2, y2, z2; you can package them up in a point(x, y, z) and just pass point1, point2. (Yes this is basic usage of structs but many languages conflate the them anyway)