Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classes vs. Functions [closed]

Functions are easy to understand even for someone without any programming experience, but with a fair math background. On the other hand, classes seem to be more difficult to grasp.

Let's say I want to make a class/function that calculates the age of a person given his/her birthday year and the current year. Should I create a class for this, or a function? Or is the choice dependant to the scenario?

P.S. I am working on Python, but I guess the question is generic.

like image 741
multigoodverse Avatar asked Aug 13 '13 07:08

multigoodverse


People also ask

What do classes and closures have in common?

Edit: As user Faisal put it, both closures and classes can be used to "describe an entity that maintains and manipulates state", so closures provide a way to program in an object oriented way using functional languages.

Is a class A group of functions?

A class is essentially a family of functions that are related to each other in some way. They may have commonality under a certain category. They may have similarities in what they do.

What is the difference between class () and typeof () functions in R?

The class function in R helps us to understand the type of object, for example the output of class for a data frame is integer and the typeof of the same object is list because data frames are stored as list in the memory but they are represented as a data frame.

What is difference between class and function in JavaScript?

One key distinction between functions and classes was highlighted in this talk which suggests that a function is a behavior that can carry data while, inversely, a class is data that can carry behavior.


1 Answers

Create a function. Functions do specific things, classes are specific things.

Classes often have methods, which are functions that are associated with a particular class, and do things associated with the thing that the class is - but if all you want is to do something, a function is all you need.

Essentially, a class is a way of grouping functions (as methods) and data (as properties) into a logical unit revolving around a certain kind of thing. If you don't need that grouping, there's no need to make a class.

like image 177
Amber Avatar answered Oct 27 '22 14:10

Amber