We’ll start with the basics: What does first-class citizenship mean in general? First-class citizenship, within the world of programming, means that a given entity (such as a function) supports all the operational properties inherent to other entities; properties such as being able to be assigned to a variable, passed around as a function argument, returned from a function, etc. Basically, first-class citizenship simply means “being able to do what everyone else can do.”
Let’s look at some examples/tests:
Can we assign a function to a variable?
Yes we can!
Can we pass a function as an argument to another function?
But can we return a function… from a function?? (Hint: We already did, but… let’s see it again!)
Yep, piece of cake!
One can get pretty creative with assigning functions to variables and passing them around to other functions from which they can be returned. If you’re not careful (or maybe if you just want to have a bit of fun!), the rabbit hole can get pretty deep, pretty quickly! Consider this… a function can be passed to itself and even returned from itself!
Thanks for reading!