2018-12-30 16:43:04 -08:00
|
|
|
---
|
|
|
|
|
title: constructs / function
|
|
|
|
|
---
|
2023-11-25 09:50:24 -08:00
|
|
|
# function
|
2018-12-30 16:43:04 -08:00
|
|
|
|
2020-03-11 23:17:48 +08:00
|
|
|
### The LPC function (or method)
|
1993-01-07 13:20:58 -05:00
|
|
|
|
|
|
|
|
The LPC function is similar but not identical to that provided by C
|
2018-12-30 16:59:01 -08:00
|
|
|
(it is most similar to that provided by ANSI C). The syntax is as follows:
|
1993-01-07 13:20:58 -05:00
|
|
|
|
2025-11-13 02:53:35 -05:00
|
|
|
```c
|
1993-01-07 13:20:58 -05:00
|
|
|
return_type function_name(arg1_type arg1, arg2_type arg2, ...)
|
|
|
|
|
{
|
2025-11-13 01:54:19 -05:00
|
|
|
// Variables can now be declared anywhere in the function body,
|
|
|
|
|
// not just at the top (C99-style)
|
2020-03-11 23:17:48 +08:00
|
|
|
variable_declarations;
|
|
|
|
|
...;
|
2018-12-30 16:59:01 -08:00
|
|
|
|
|
|
|
|
statements;
|
|
|
|
|
...;
|
1993-01-07 13:20:58 -05:00
|
|
|
|
2025-11-13 01:54:19 -05:00
|
|
|
more_variable_declarations; // Declarations can be interspersed
|
|
|
|
|
...;
|
|
|
|
|
|
2020-03-11 23:17:48 +08:00
|
|
|
return var0;
|
1993-01-07 13:20:58 -05:00
|
|
|
}
|
2020-03-11 23:17:48 +08:00
|
|
|
```
|
1993-01-07 13:20:58 -05:00
|
|
|
|
|
|
|
|
Note that var0 must be of return_type.
|
|
|
|
|
|
|
|
|
|
If a function doesn't need to return a value, then it should be declared
|
2018-12-30 16:59:01 -08:00
|
|
|
with a return_type of "void". E.g.
|
1993-01-07 13:20:58 -05:00
|
|
|
|
2025-11-13 02:53:35 -05:00
|
|
|
```c
|
1993-01-07 13:20:58 -05:00
|
|
|
void function_name(arg1_type arg1, ...)
|
|
|
|
|
{
|
2020-03-11 23:17:48 +08:00
|
|
|
statements;
|
|
|
|
|
...;
|
1993-01-07 13:20:58 -05:00
|
|
|
}
|
2020-03-11 23:17:48 +08:00
|
|
|
```
|
1993-01-07 13:20:58 -05:00
|
|
|
|
|
|
|
|
Invoke a function as follows:
|
|
|
|
|
|
2020-03-11 23:17:48 +08:00
|
|
|
function_name(arg1, arg2, arg3, ...);
|
1993-01-07 13:20:58 -05:00
|
|
|
|
|
|
|
|
You may invoke a function in another object as follows:
|
|
|
|
|
|
2020-03-11 23:17:48 +08:00
|
|
|
object->function_name(arg1, arg2, arg3, ...);
|
1993-01-07 13:20:58 -05:00
|
|
|
|
|
|
|
|
or:
|
|
|
|
|
|
2020-03-11 23:17:48 +08:00
|
|
|
call_other(object, function_name, arg1, arg2, ...);
|
2025-11-13 02:53:35 -05:00
|
|
|
|
|
|
|
|
### First-Class Functions
|
|
|
|
|
|
|
|
|
|
Functions can be treated as first-class values and stored in variables. You can
|
|
|
|
|
use function variables to implement callbacks, strategy patterns, and functional
|
|
|
|
|
programming techniques.
|
|
|
|
|
|
|
|
|
|
**Storing functions:**
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
function f = add; // Store function reference
|
|
|
|
|
int result = f(5, 3); // Call it later
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Passing functions as arguments:**
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
void apply_operation(int a, int b, function op) {
|
|
|
|
|
int result = op(a, b);
|
|
|
|
|
printf("Result: %d\n", result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int add(int x, int y) { return x + y; }
|
|
|
|
|
int multiply(int x, int y) { return x * y; }
|
|
|
|
|
|
|
|
|
|
void create() {
|
|
|
|
|
apply_operation(5, 3, add); // Result: 8
|
|
|
|
|
apply_operation(5, 3, multiply); // Result: 15
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Returning functions:**
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
function get_comparator(string type) {
|
|
|
|
|
if (type == "ascending") return (: $1 - $2 :);
|
|
|
|
|
if (type == "descending") return (: $2 - $1 :);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sort_by_type(int *arr, string type) {
|
|
|
|
|
function cmp = get_comparator(type);
|
|
|
|
|
return sort_array(arr, cmp);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Storing in data structures:**
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
// Array of functions
|
|
|
|
|
function *operations = ({ add, subtract, multiply, divide });
|
|
|
|
|
int result = operations[0](10, 5);
|
|
|
|
|
|
|
|
|
|
// Mapping of functions
|
|
|
|
|
mapping commands = ([
|
|
|
|
|
"attack": (: do_attack :),
|
|
|
|
|
"defend": (: do_defend :),
|
|
|
|
|
"heal": (: do_heal :),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
void execute_command(string cmd) {
|
|
|
|
|
function handler = commands[cmd];
|
|
|
|
|
if (handler) handler();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2026-07-11 22:45:47 -04:00
|
|
|
### Pass-by-Reference Parameters
|
|
|
|
|
|
|
|
|
|
Functions can accept parameters by reference using the `ref` keyword (or its
|
|
|
|
|
shorthand `&`). Changes to a ref parameter inside the function are reflected in
|
|
|
|
|
the caller's variable:
|
|
|
|
|
|
|
|
|
|
```c
|
|
|
|
|
void increment(int ref value) {
|
|
|
|
|
value++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int x = 10;
|
|
|
|
|
increment(ref x); // x is now 11
|
|
|
|
|
increment(& x); // x is now 12
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
See `constructs/ref` for full details.
|
|
|
|
|
|
2025-11-13 02:53:35 -05:00
|
|
|
See also: `types/function` for more details on function pointers and syntax.
|