> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin External Function

An `external` function in Kotlin is a function declaration that delegates its implementation to a foreign language or runtime environment. It acts as a binding mechanism, instructing the Kotlin compiler that the actual execution logic will be resolved at runtime from an outside source, such as a compiled C/C++ library or a JavaScript module. It is the direct Kotlin equivalent of the `native` keyword in Java.

## Syntax

The `external` modifier is placed before the `fun` keyword. Because the implementation exists outside the Kotlin source, the function declaration must not contain a body.

```kotlin theme={"dark"}
external fun computeHash(input: String): ByteArray
```

External functions can be declared as top-level functions, member functions within a class, or extension functions.

```kotlin theme={"dark"}
class NativeProcessor {
    // Member external function
    external fun processData(buffer: ByteArray): Int
}

// Extension external function
external fun String.toNativeString(): Long
```

## Technical Characteristics

* **No Body:** An `external` function cannot have a block body `{ ... }` or an expression body `= ...`. Attempting to provide one results in a compilation error.
* **Property Accessors:** The `external` modifier can be applied to property getters and setters. To target an accessor, the `external` modifier must be placed directly on the `get` or `set` keyword itself, rather than on the property declaration.

```kotlin theme={"dark"}
val systemTime: Long
    external get
    
var hardwareState: Int
    external get
    external set
```

*(Note: In Kotlin/JS, the `external` modifier can be applied directly to a property declaration, but such properties are strictly prohibited from having custom accessors declared.)*

* **Visibility and Modifiers:** `external` functions support standard visibility modifiers (`private`, `internal`, `public`) and can be combined with other modifiers like `open` or `override` when participating in inheritance hierarchies.

## Platform-Specific Resolution

The mechanism by which the Kotlin compiler and runtime resolve the `external` function depends on the compilation target:

**Kotlin/JVM**
On the JVM, `external` functions rely on the Java Native Interface (JNI). The Kotlin compiler generates bytecode identical to a Java `native` method. The corresponding C or C++ implementation must follow JNI naming conventions (e.g., `Java_com_example_NativeProcessor_processData`) and the shared library containing the implementation must be loaded into the JVM memory space, typically using an `init` block:

```kotlin theme={"dark"}
class NativeProcessor {
    init {
        System.loadLibrary("processor_lib")
    }
    external fun processData(buffer: ByteArray): Int
}
```

**Kotlin/JS**
In Kotlin/JS, the `external` modifier informs the compiler that the function is implemented in JavaScript. The compiler does not perform name mangling on external declarations, ensuring the Kotlin signature maps directly to the underlying JavaScript function. It is frequently combined with annotations like `@JsModule` and `@JsNonModule` to define the exact module resolution path.

```kotlin theme={"dark"}
@JsModule("crypto-js")
@JsNonModule
external fun md5(message: String): String
```

**Kotlin/Native**
For Kotlin/Native, `external` functions are resolved using the `cinterop` tool. The compiler parses C header files (`.h`) to generate Kotlin bindings, allowing the `external` Kotlin function to map directly to the statically or dynamically linked LLVM bitcode of the C/Objective-C implementation.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
