---
name: "libmawk_register_function"
section: "3libmawk"
platform: "linux"
package: "libmawk1t64"
version: "1.0.4-4"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/libmawk_register_function"
---

# libmawk\_register\_function(3libmawk)

registers a C function with a callback

## NAME

libmawk\_register\_function - registers a C function with a callback

## SYNOPSIS

```
#include <libmawk.h>

typedef CELL *libmawk_c_function(mawk_state_t *m, CELL *sp, int a_args);
```

**int libmawk\_register\_function(mawk\_state\_t \****MAWK***, const char \****fname***, libmawk\_c\_function \****callback***);** **CELL \*libmawk\_stackret(CELL \****original\_sp***);**

## DESCRIPTION

The **libmawk\_register\_function**() call registers an user defined function donated by the host application in a mawk context so that it acts exactly like user defined functions in  written in awk. The name of the new function is given in *fname* and should not match any of the user defined function names in the awk script.

When the user function is called back, argument *sp* is the stack pointer and *a\_args* holds the number of arguments. The user function is responsible for managing the stack: it should pop all arguments before returning.

The user function should also generate a return value, which is done by calling libmawk\_set\_cell() on the stack slot returned by libmawk\_stackret. Libmawk\_stackret should be called with the modified *sp* after popping all arguments.

Argument m is a libmawk context previously returned by libmawk\_initialize() or libmawk\_initialize\_stage3().

For more information about user function callbacks, especially on stack handling, see manual page example(3libmawk).

## RETURN VALUE

The user function should return the stack pointer after popping all arguments.

The libmawk\_register\_function call returns 0 on success.

Call libmawk\_stackret returns a stack pointer to the slot where the user function should store its return value.

## SEE ALSO

**libmawk\_initialize\_stage**(3libmawk), **libmawk\_initialize**(3libmawk), **libmawk\_set\_cell**(3libmawk), **libmawk\_print\_cell**(3libmawk).
