Skip to main content

First-Class Functions

Pranav-0440
EditReport

1. Introductionโ€‹

A programming language is said to have First-Class Functions when functions in that language are treated like any other variable. In other words, functions are treated as First-Class Citizens.

This means that functions can be:

  1. Assigned as a value to a variable or constant.
  2. Passed as an argument to another function.
  3. Returned from another function as a result.

Having first-class functions is a prerequisite for functional programming, enabling powerful design patterns like callbacks, currying, and closures.


2. The Three Key Criteria with Examplesโ€‹

Let's look at how JavaScript, Python, and C++ satisfy the three criteria.

2.1 Criterion 1: Assigning Functions to Variablesโ€‹

JavaScriptโ€‹

const greet = function(name) {
return `Hello, ${name}!`;
};

console.log(greet("Alice")); // Output: Hello, Alice!

Pythonโ€‹

def greet(name):
return f"Hello, {name}!"

say_hello = greet # Assigning the function object to another variable
print(say_hello("Bob")) # Output: Hello, Bob!

C++ (C++11+)โ€‹

#include <iostream>
#include <functional>
#include <string>

std::string greet(std::string name) {
return "Hello, " + name + "!";
}

int main() {
// Assigning function pointer using std::function
std::function<std::string(std::string)> sayHello = greet;
std::cout << sayHello("Charlie") << std::endl; // Output: Hello, Charlie!
return 0;
}

2.2 Criterion 2: Passing Functions as Argumentsโ€‹

JavaScriptโ€‹

function executeFunction(fn, value) {
return fn(value);
}

const square = x => x * x;
console.log(executeFunction(square, 5)); // Output: 25

Pythonโ€‹

def execute_function(fn, value):
return fn(value)

def square(x):
return x * x

print(execute_function(square, 5)) # Output: 25

C++โ€‹

#include <iostream>
#include <functional>

void run(std::function<int(int)> fn, int value) {
std::cout << "Result: " << fn(value) << std::endl;
}

int main() {
auto square = [](int x) { return x * x; };
run(square, 5); // Output: Result: 25
return 0;
}

2.3 Criterion 3: Returning Functions from Functionsโ€‹

JavaScriptโ€‹

function multiplier(factor) {
return function(number) {
return number * factor;
};
}

const double = multiplier(2);
console.log(double(10)); // Output: 20

Pythonโ€‹

def multiplier(factor):
def multiply(number):
return number * factor
return multiply

double = multiplier(2)
print(double(10)) # Output: 20

C++โ€‹

#include <iostream>
#include <functional>

std::function<int(int)> multiplier(int factor) {
return [factor](int number) {
return number * factor;
};
}

int main() {
auto doubleFunc = multiplier(2);
std::cout << doubleFunc(10) << std::endl; // Output: 20
return 0;
}

3. First-Class Functions vs. Higher-Order Functionsโ€‹

It is common to confuse First-Class Functions with Higher-Order Functions.

  • First-Class Functions is a property of the programming language. It means the language syntax permits functions to behave like regular values.
  • Higher-Order Functions is a functional programming concept. It refers to functions that take other functions as arguments or return them.

Analogy: You cannot easily write Higher-Order Functions in a language unless it supports First-Class Functions. First-Class Functions are the tools, and Higher-Order Functions are what you build with them.


4. Video Explanationโ€‹

Telemetry Integration

Completed working through this block? Sync progress to workspace.