Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

// request.cf · coarse context

A page that knows where it met you.

Only coarse request metadata is shown. This demo does not display or persist visitor IP addresses.

Country
US
Cloudflare location
CMH
Connection
HTTP/2
Language
Not provided

Ray ID: a21afa6c3eb55a3a

Jump to content

// Workers AI · dad joke modeWhat did the dispatch table say? "I've got a route to the answer.

From Wikipedia, the free encyclopedia
(Redirected from Function table)

In computer science, a dispatch table is a data structure that maps a series of known actions to their corresponding code, which can later be retrieved dynamically. This structure is commonly implemented either using an associative array to directly map method names to a corresponding function pointer, or a simple array of function pointers with known indices.

Common use cases include the implementation of late binding and virtual polymorphism in object-oriented programming and the implementation of system calls.[1]

Use cases

[edit]

Virtual methods

[edit]

In object-oriented programming languages that support virtual methods, the compiler will automatically create a dispatch table for each class that maps method names to their corresponding code.[2] This table is called a virtual method table or vtable, and a pointer to the table is stored in every object created from that class, called a virtual pointer or vptr.[3] Since an object always has direct access to its methods using the vptr, virtual methods can be dynamically called at runtime without needing to know the specific class that the object is constructed from. This layer of indirection makes polymorphism and late binding possible, which is a fundamental aspect of object-oriented programming.

System calls

[edit]

In operating system kernel design, system calls are commonly implemented using a dispatch table of indices mapping to corresponding privileged system actions. For example, in Linux, the sys_exit system call has an index of 60, and can be called by passing 60 into the appropriate processor register and triggering a program interrupt.[4]

; Calls sys_exit with exit code 0
mov rax, 60
mov rdi, 0
syscall

Since system calls only require a number to reference the underlying kernel action, they can be called from user space while in reality triggering code in kernel space. This improves system security by ensuring that privileged code can only be run through the narrow interface that each call provides.[5]

Example implementations

[edit]

Perl

[edit]

The following shows one way to implement a dispatch table in Perl, using a hash to store code references (also known as function pointers).[6]

# Define the table using one anonymous code-ref and one named code-ref
my %dispatch = (
    "-h" => sub { return "hello\n"; },
    "-g" => \&say_goodbye
);
 
sub say_goodbye {
    return "goodbye\n";
}
 
# Fetch the code ref from the table, and invoke it
my $sub = $dispatch{$ARGV[0]};
print $sub ? $sub->() : "unknown argument\n";

Running this Perl program as perl greet -h will produce "hello", and running it as perl greet -g will produce "goodbye".

JavaScript

[edit]

A dispatch table can be implemented in JavaScript by using an object to store anonymous function definitions.[7]

// Dispatch table of actions as well as a fallback
const actions = {
    action1() { console.log("action1 called"); },
    action2() { console.log("action2 called"); },
    action3() { console.log("action3 called"); },
    fallback() { console.log("fallback called"); }
};

function performAction(key) {
    const action = Object.hasOwn(actions, key) ? key : "fallback";
    return actions[action]();
}

performAction("action1"); // outputs "action1 called"
performAction("action3"); // outputs "action3 called"
performAction("action5"); // outputs "fallback called"

Lua

[edit]

In Lua, tables can store any value, including functions, making it trivial to implement a simple dispatch table.[8][9]

-- Dispatch table of actions as well as a fallback
local actions = {
  action1 = function ()
    print('action1 called')
  end,
  action2 = function ()
    print('action2 called')
  end,
  action3 = function ()
    print('action3 called')
  end,
  fallback = function ()
    print('fallback called')
  end
}

function perform_action(key)
    if actions[key] ~= nil then 
        return actions[key]()
    end
    return actions.fallback()
end

perform_action('action1') -- outputs "action1 called"
perform_action('action3') -- outputs "action3 called"
perform_action('action5') -- outputs "fallback called"

See also

[edit]

References

[edit]
  1. Goldfuss, Alice. "Function Dispatch Tables in C". alicegoldfuss.com. Retrieved 23 January 2021.
  2. Vitek, Jan (1996). "Compact Dispatch Tables for Dynamically Typed Object Oriented Languages" (PDF). Retrieved 2026-07-21.{{cite web}}: CS1 maint: url-status (link)
  3. Zhegalova, Elizaveta (2026-04-30). "Silent foe or quiet ally: Brief guide to alignment in C++. Part 3". PVS-Studio. Retrieved 2026-07-22.
  4. "System Calls — The Linux Kernel documentation". linux-kernel-labs.github.io. Archived from the original on 2019-08-26. Retrieved 2026-07-22.
  5. Hein, Kyaw Soe (2025-09-01). "Kernel space, User space and System calls". Medium. Retrieved 2026-07-22.
  6. Farrell, David (2013-07-07). "Re-use code with Perl's anonymous functions". Perl.com. Retrieved 2026-07-22.
  7. "Functions - JavaScript | MDN". MDN Web Docs. 2025-07-08. Retrieved 2026-07-22.
  8. "Programming in Lua : 2.5". www.lua.org. Retrieved 2026-07-22.
  9. "Lua Functions in Tables". Programming. 2023-08-27. Retrieved 2026-07-22.