Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are functions not considered first class citizens in C

Tags:

c

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 structs 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)

like image 999
Clearer Avatar asked Jan 04 '18 09:01

Clearer


People also ask

What does it mean for a function to be a first-class citizen?

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.

Are functions first class in scheme?

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.

Are functions first class citizens in Rust?

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.

Why are functions considered first-class objects?

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.


Video Answer


2 Answers

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:

  1. It can be used, without restriction, wherever "ordinary" values can, i.e., passed and returned from functions, put in containers, etc.

  2. It can be constructed, without restriction, wherever "ordinary" values can, i.e., locally, in an expression, etc.

  3. 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

like image 130
hnefatl Avatar answered Sep 28 '22 23:09

hnefatl


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.

like image 29
jforberg Avatar answered Sep 29 '22 01:09

jforberg