Random Number GodsRNG · Enemy Territory · Battlefield · D&D
Friday BF64 + Sunday D&D

Check the calendar. ET pubs running the new Frostbite rotation all week.

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

Permalink Preview Print Guide
TheMightyElephant
Field Medic
Members
25 posts 11 Aug 2020 joined
7 Sep 2026 at 16:01 #1

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.

TheMightyElephant - BF64s
PTFO or mute.

maltonik
Field Medic
Members
28 posts 31 Mar 2022 joined
7 Sep 2026 at 16:06 #2

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

maltonik - Map votes
Frostbite supremacy.

AuthorSei-chan
Field Medic
Members
34 posts 31 Mar 2020 joined
7 Sep 2026 at 16:06 #4

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

AuthorSei-chan - Trial / new blood
Still learning the smokes.

Braddock512
Engineer
Members
48 posts 1 Mar 2018 joined
7 Sep 2026 at 16:08 #5

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.

Braddock512 - ET pubs
See you on rng-et-1.

draco1x1
Field Medic
Members
34 posts 7 Jun 2024 joined
7 Sep 2026 at 16:08 #6

you know, all the stuff m'kay

draco1x1 - Map votes
Frostbite supremacy.

fructe
Covert Ops
Members
63 posts 7 Jun 2019 joined
7 Sep 2026 at 16:08 #7

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

fructe - NA nights
I take the dead hours.

sega0719
Engineer
Members
39 posts 25 Dec 2021 joined
7 Sep 2026 at 16:08 #8

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.

sega0719 - Allies rifle
Spawn on me.

kubNfYN
Covert Ops
Members
65 posts 1 Apr 2025 joined
7 Sep 2026 at 16:09 #9

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

kubNfYN - Axis defense
Do not walk my MG line twice.

zabak31
Field Medic
Members
34 posts 16 Nov 2025 joined
7 Sep 2026 at 16:10 #10

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.

zabak31 - BF support
Ammo box is friendship.

Feran
Engineer
Members
41 posts 2 Oct 2024 joined
7 Sep 2026 at 16:12 #11

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.

Feran - Comp flex
Put me where you need a body.

Havrik3434
Engineer
Members
37 posts 14 Oct 2022 joined
7 Sep 2026 at 16:12 #12

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

Havrik3434 - Comp flex
Put me where you need a body.

kerentase
Covert Ops
Members
54 posts 5 May 2021 joined
7 Sep 2026 at 16:13 #13

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)

kerentase - Trial / new blood
Still learning the smokes.

Sign in to reply.