In C (and so many other "low-level" languages), functions have a type. You can declare a variable with a type that matches a function and can assign a function to such a variable, yet people insists that functions are not first class citizen in C.
Using pointers to functions and passing functions to other functions as arguments is key in several standard functions in C; qsort
for example, require a comparison function to be passed or it can't do anything.
It is not uncommon to see "object-oriented" programming in C, by declaring a struct
with several variables with function-types. Callbacks can be and often are (if not always -- I can't imagine any other way to do it) implemented using variables with function-types or struct
s with members with function types.
So, why aren't functions considered first class citizens in C?
(I'm sure this is duplicate, but I can't seem to find any similar questions here)
If any programming language has the ability to treat functions as values, to pass them as arguments and to return a function from another function then it is said that programming language has First Class Functions and the functions are called as First Class Citizens in that programming language.
In functional and semi-functional languages including Scheme, Javascript, Haskell, ML, functions are First Class Citizens , meaning that, like any other value, they can be passed as arguments to other functions.
We say that Rust has first-class functions because we can pass them around and use them like we can with other values. We say bar is a higher-order function because it takes a function as an argument, i.e., it is a function that operates on functions.
A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.
There's a really good answer by Andreas Rossberg on a Scala question (near-duplicate) that happens to explain why functions in C/C++ aren't first class functions. To quote:
Being "first-class" is not a formally defined notion, but it generally means that an entity has three properties:
It can be used, without restriction, wherever "ordinary" values can, i.e., passed and returned from functions, put in containers, etc.
It can be constructed, without restriction, wherever "ordinary" values can, i.e., locally, in an expression, etc.
It can be typed in a way similar to "ordinary" values, i.e., there is a type assigned to such an entity, and it can be freely composed with other types.
For functions, (2) particularly implies that a local function can use all names in scope, i.e. you have lexical closures. It also often comes with an anonymous form for construction (such as anonymous functions), but that is not strictly required (e.g. if the language has general enough let-expressions). Point (3) is trivially true in untyped languages.
...
- Functions in C/C++ are not first-class. While (1) and (3) are arguably available through function pointers, (2) is not supported for functions proper. (A point that's often overlooked.)
Emphasis mine
Adding an example to the theory explanation by hnefatl. In C, int values are first class. We can add two int values, creating a new int value:
int add(int a, int b)
{
return a + b;
}
Similarly in Haskell, we can compose two functions (of appropriate type) to create a new function:
compose f g = f . g
h = compose f g is the function such that h(x) = f(g(x)).
Such an operation can't be done in C, to my knowledge. The same effect can be achieved through other means in C of course. But we cannot compute new functions as easily and naturally as we can numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With