function_exists
function_exists(string $function): bool
Checks whether a function is defined by name (a common tool for extension checks and feature detection).
C C23
#include <stdio.h>
static void greet(void) {}
int main(void) {
/* link-time binding: a missing function is a link error, not a false at runtime */
void (*fp)(void) = greet; /* designator decays to pointer */
printf("%d\n", fp != NULL); /* taking its address is the only runtime fact */
return 0;
}
1
C++ C++26
#include <iostream>
void greet() {}
int main() {
// link-time binding like C; compile-time detection needs SFINAE or concepts
auto fp = static_cast<void (*)()>(&greet);
std::cout << std::boolalpha << (fp != nullptr) << "\n";
return 0;
}
true
C# v11.0.100
using System.Reflection;
class Program {
static void Greet() {}
static void Main() {
// reflection: query by name at runtime
var flags = BindingFlags.NonPublic | BindingFlags.Static;
Console.WriteLine(typeof(Program).GetMethod("Greet", flags) != null);
Console.WriteLine(typeof(Program).GetMethod("Nope", flags) != null);
}
}
True False
Go v1.27.4
package main
import "fmt"
// no runtime lookup for functions; a map registry is the idiom
func greet() {}
var funcs = map[string]func(){"greet": greet}
func main() {
_, okG := funcs["greet"]
_, okN := funcs["nope"]
fmt.Println(okG, okN)
}
true false
Java v27.0.1
public class Main {
static void greet() {}
static boolean exists(String name) {
try {
Main.class.getDeclaredMethod(name); // reflection: query by name
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
public static void main(String[] args) {
System.out.println(exists("greet") + " " + exists("nope"));
}
}
true false
JavaScript v26.13.1
function greet() {}
// typeof is the only form safe on undeclared names
console.log([typeof greet === "function", typeof nope === "function"]);
[ true, false ]
PHP v8.6.0
function greet() {}
echo json_encode([function_exists('greet'), function_exists('nope')]);
[true,false]
Python v3.15.2
def greet(): ...
# objects + globals(): callable-by-name lookup
print([callable(globals().get("greet")), callable(globals().get("nope"))])
[True, False]
Rust v1.99.1
fn greet() {}
fn main() {
// compile-time binding: a missing fn fails compilation; a registry emulates lookup
let registry: [(&str, Option<fn()>); 2] =
[("greet", Some(greet as fn())), ("nope", None)];
for (name, f) in registry {
println!("{} {}", name, f.is_some());
}
}
greet true nope false
TypeScript v26.13.1
function greet(): void {}
// typeof is the only form safe on undeclared names (the static checker flags `nope`)
console.log([typeof greet === "function", typeof nope === "function"]);
[ true, false ]
Language differences
C: no runtime lookup — referencing a missing function is a link error (the example can only show the "exists" side); probing whether an API exists relies on preprocessor macros (#ifdef) or dlsym dynamic loading.
C++: the same link-time binding as C (example); library feature detection uses __has_include or SFINAE/concepts at compile time — there is no runtime function table.
C#: reflection Type.GetMethod queries by name (example); remember BindingFlags for non-public methods; assembly-level probes use Assembly.GetType + the same technique.
Go: functions have no runtime name — build a map[string]func() registry yourself (example) and use comma-ok as the existence check; plugin scenarios need the plugin package or an external process.
Java: reflection getDeclaredMethod queries by name (example) and throws NoSuchMethodException on a miss — a try/catch turns the "exception" into false; for extension classes probe with Class.forName first.
JavaScript: typeof name === "function" (example) is the only probe that never throws on an undeclared identifier; window[name]/globalThis[name] can find global functions but not module-scoped ones.
PHP: function_exists is the feature-detection staple (e.g. checking an extension: function_exists('curl_init')); note it only sees global functions — use method_exists for class methods.
Python: functions are objects in globals() (example); hasattr(module, name) is the object-side counterpart; the standard idiom for feature probing is try/except ImportError.
Rust: compile-time binding, missing means compile error (the example shows the runtime-queryable shape via an Option<fn> registry); cfg feature detection happens at compile time.
TypeScript: runtime matches JavaScript (example); statically nope is flagged as undefined — the proper approach is an explicit optional type, not runtime probing.