DEV Community

Cover image for Functions are First-Class Citizens — What Does It Mean?
Devashish Roy
Devashish Roy

Posted on

Functions are First-Class Citizens — What Does It Mean?

Before understanding why functions are called first-class citizens in some programming languages and not in others, we need to understand what a first-class citizen means in a programming language.


What is a First-Class Citizen?

In programming, a first-class citizen (or first-class object) is an entity that can be treated like any other value in the language.

This means it can:

  1. Be assigned to a variable.
  2. Be passed as an argument to a function.
  3. Be returned from a function.
  4. Be stored in data structures (like lists, maps, etc.).

Example — Functions as First-Class Citizens (Python)

def greet():
    return "Hello"

# 1. Assigned to a variable
say_hello = greet

# 2. Passed as an argument
def call_func(func):
    print(func())

call_func(say_hello)

# 3. Returned from a function
def outer():
    return greet

inner = outer()
print(inner())
Enter fullscreen mode Exit fullscreen mode

Here, functions are first-class citizens because they can be assigned, passed, and returned.


Who Are the First-Class Citizens in Popular Languages?

C

  • ✅ Primitive types (int, float, char)
  • ✅ Pointers (including function pointers)
  • ⚠️ Functions are not fully first-class — you can use function pointers, but no closures or nested functions.

    → First-class citizens: Variables, pointers, primitive types

C++

  • ✅ Objects (instances of classes)
  • ✅ Primitive types
  • ✅ Lambdas / function objects (std::function) since C++11
  • ⚠️ Regular functions are not first-class by themselves

    → First-class citizens: Objects, primitive types, lambdas, std::function

Java

  • ✅ Objects (instances of classes)
  • ✅ Lambdas and method references since Java 8
  • ⚠️ Regular methods are not first-class

    → First-class citizens: Objects, lambdas (Java 8+)

JavaScript

  • ✅ Functions (assignable, passable, returnable)
  • ✅ Objects, arrays, primitives
  • ✅ Closures supported

    → First-class citizens: Functions, objects, primitives

TypeScript

  • ✅ Superset of JavaScript, same rules
  • ✅ Functions, objects, primitives, classes
  • ✅ Type annotations make function-as-value more explicit

    → First-class citizens: Functions, objects, primitives, classes

Python

  • ✅ Everything is an object
  • ✅ Functions, classes, modules, numbers, strings — all first-class

    → First-class citizens: Everything (especially functions and classes)

Go (Golang)

  • ✅ Functions (assignable, passable, returnable)
  • ✅ Interfaces, structs, primitives
  • ✅ Closures supported

    → First-class citizens: Functions, structs, interfaces, primitives


First-Class Entities Matrix with Examples

Language Entity Assigned Passed Returned Stored Created
C Primitive (int,char)
Pointer
Struct
Function ⚠️ ⚠️ ⚠️ ⚠️
C++ Primitive
Object
Pointer / Reference
Lambda / std::function
Java Primitive
Object
Lambda / Functional Interface
Class
JavaScript Function
Object
Array
Class
Primitive
Python Function
Object
Class
Module
Primitive
Go Function
Struct
Interface
Primitive

Summary Table

Language First-Class Citizens
C Variables, pointers, primitive types
C++ Objects, primitive types, lambdas (std::function)
Java Objects, lambdas (Java 8+)
JavaScript Functions, objects, primitives
TypeScript Functions, objects, primitives, classes
Python Everything (functions, classes, modules, etc.)
Go Functions, structs, interfaces, primitives

Why Functions Are First-Class Citizens in Some Languages

Functions are first-class citizens in languages like Python, JavaScript, Go, and TypeScript because they satisfy all the properties of first-class entities:

  • They can be assigned to variables
  • They can be passed as arguments
  • They can be returned from other functions
  • They can be stored in data structures
  • They can be created dynamically

In languages like C or Java (pre-Java 8), functions are not fully first-class because they cannot satisfy all these properties — e.g., you cannot create functions dynamically or return raw functions as easily.


In short: A first-class function is a function that can be treated just like any other value.

Languages differ in their support, and that’s why understanding first-class citizens helps you write more flexible, functional, and expressive code.

Top comments (0)