Class vs Function

I provided a few links for you. Did you already read those, and do you have more specific questions after that?

A class is like a blueprint that is used to create objects in memory.

You might have a blueprint of a car (class) and use it to build 5 cars (objects). Each car will maintain it’s own mileage or different color independently but will work the same since they have shared blueprints.

A class can have functions within it (called methods) as well as internal data that will be stored in each instantiation of that class. The car class might have a changeTires() method.

I think a function is more straightforward as you described, a block of code that gets executed when you call it. You don’t really call a class but you create an object (instantiate) from it.

So, from these examples I hope it gives you a better understanding of when you want to use each? I think this is something that will become clear to you after time spent using both classes and functions.

You can find similar explanations here:

https://forum.freecodecamp.org/t/transition-from-functional-programming-to-oop/689060/1

https://forum.freecodecamp.org/t/oop-for-beginners/640776/1

2 Likes