.\" Made by website/plain/build.mjs from website/words/void.md; change that file. .TH SG-VOID 7 2026-11-28 "SG" "SG reference" .SH NAME void \- the return type of a function that gives nothing back .SH SYNOPSIS .TP \fBvoid f()\fR a function that gives nothing back; a call of it is a statement .TP \fBreturn\fR ends a void function early .TP \fBvoid main(), void main(List args)\fR a program that ends with code 0, or with exit(code) .TP \fBResult\fR success with nothing in it, written return Ok() .TP \fBasync void f()\fR an async function whose call gives a Task .TP \fBFunc, Func\fR a function that takes an int, or nothing, and gives nothing .TP \fBvoid srand(uint seed)\fR in an extern block, a function of C that gives nothing .TP \fBCFunc\fR a pointer to such a function, as C calls it .TP \fBraw void\fR an address of memory of no known type, C's void* .SH DESCRIPTION \fBvoid\fR in front of a function says it gives nothing back. Its body ends at its last line, or earlier with a bare \fBreturn\fR; a \fBreturn\fR with a value is an error, and the compiler asks for no \fBreturn\fR on any path. A call of it is a statement of its own. It has no value, so it is not kept in a variable, passed on, compared or dropped with \fB_ =\fR\&. .PP \fBvoid\fR is not a type of a value. No variable, parameter, field or item of a list has it. It stands where a function's result is named: in front of the function, as the last type of a \fBFunc\fR or a \fBCFunc\fR, and inside \fBResult\fR and \fBTask\fR, the result of a function that can fail or that runs as a task, with nothing to give on success. \fBraw void\fR is the one other place: the address of memory whose type is not known. .PP \fBmain\fR is \fBvoid main()\fR, or \fBvoid main(List args)\fR to see the command line. A program whose \fBmain\fR ends normally exits with code 0; \fBexit(code)\fR ends it with any other. \fBmain\fR does not return a number: a program that can fail is written \fBResult main()\fR, and an error it returns is printed and gives code 1. .PP \fBasync void f()\fR is a function that runs as a task: its call gives a \fBTask\fR, which is awaited like any other and ends when the body does. .SH EXAMPLES .SS "A function that gives nothing back" .RS 4 .nf struct Counter { int count void add(ref this, int n) { if n <= 0 { return } this.count += n } } void report(read Counter c) { print($"{c.count} so far") } void main() { var c = Counter { count = 0 } c.add(5) c.add(-2) c.add(3) report(read c) } .fi .RE .PP .RS 4 .nf 8 so far .fi .RE .PP \fBadd\fR leaves early with a bare \fBreturn\fR when there is nothing to add, so \fB\-2\fR changes nothing. Each call of a \fBvoid\fR function is a line of its own. .SS "Success with nothing in it" .RS 4 .nf Result check(read string name) { if name.length() == 0 { return Error("the name is empty".clone()) } if name.length() > 8 { return Error($"`{name}` is too long".toString()) } return Ok() } Result checkAll(read List names) { for name in read names { try check(name) } return Ok() } void main() { var good = List { "Ada", "Grace" } var bad = List { "Ada", "Montgomery" } print(checkAll(read good) is Ok) match checkAll(read bad) { Ok => print("all fine") Error(why) => print(why) } } .fi .RE .PP .RS 4 .nf true `Montgomery` is too long .fi .RE .PP A check that only says yes or no returns a \fBResult\fR: \fBOk()\fR carries nothing. \fBtry\fR passes an error on. As a pattern, in \fBis\fR and in \fBmatch\fR, it is written \fBOk\fR, since there is nothing in it to name, or \fBOk()\fR, as it is built. .SS "main and the exit code" The file greet/main.sg: .PP .RS 4 .nf void main(List args) { if args.length() == 0 { printError("give a name") exit(2) } print($"hello, {args[0]}") } .fi .RE .PP .RS 4 .nf $ cd greet $ sg main.sg; echo "exit code $?" give a name exit code 2 $ sg main.sg Ada; echo "exit code $?" hello, Ada exit code 0 .fi .RE .PP A \fBvoid main\fR that reaches its end gives code 0. \fBexit(2)\fR ends the program at once with code 2. .SS "Functions as values" .RS 4 .nf void twice(Func action, int value) { action(value) action(value * 2) } void shout(int n) { print($"{n}!") } void whisper(int n) { print($"({n})") } void sayDone() { print("done") } void main() { var actions = List> { shout, whisper } for action in read actions { twice(action, 21) } Func done = sayDone done() } .fi .RE .PP .RS 4 .nf 21! 42! (21) (42) done .fi .RE .PP The last type of a \fBFunc\fR is what the function gives, and \fBvoid\fR says nothing; \fBFunc\fR takes nothing and gives nothing. Both are values that can go into a list. .SS "An async function" .RS 4 .nf async void brew(string drink, int ms) { await Task.delay(ms) print($"{drink} is ready") } async void main() { var tea = brew("tea".clone(), 30) Task coffee = brew("coffee".clone(), 10) await coffee await tea print("both done") } .fi .RE .PP .RS 4 .nf coffee is ready tea is ready both done .fi .RE .PP A call of \fBbrew\fR without \fBawait\fR starts it as a task and gives a \fBTask\fR\&. \fBawait\fR waits for its end, and gives nothing. .SS "A function of C that gives nothing" The file each/native.c: .PP .RS 4 .nf #include #include void each(const int32_t *items, size_t count, void (*visit)(int32_t, void *), void *state) { for (size_t i = 0; i < count; i++) { visit(items[i], state); } } .fi .RE .PP The file each/main.sg: .PP .RS 4 .nf extern "./native.c" { void each(raw int items, nuint count, CFunc visit, raw void state) } export void add(int value, raw void state) { var total = unsafe (ref long)state total += value print($"C gave {value}") } void main() { var numbers = List { 4, 8, 15 } long total = 0 var state = unsafe Memory.addressOf(ref total) unsafe each(numbers.pointer(), (nuint)numbers.length(), add, state) print(total) } .fi .RE .PP .RS 4 .nf $ cd each $ sg main.sg C gave 4 C gave 8 C gave 15 27 $ sg build main.sg -o each && ./each C gave 4 C gave 8 C gave 15 27 .fi .RE .PP C's \fBvoid\fR is SG's \fBvoid\fR, and C's \fBvoid*\fR is \fBraw void\fR\&. \fBeach\fR gives nothing, and calls back \fBadd\fR, an \fBexport void\fR function, through a \fBCFunc\fR\&. The state goes to C as a \fBraw void\fR, and \fBunsafe (ref long)state\fR lends it again inside the callback. .SH "WHAT THE COMPILER REFUSES" .SS "The value of a void call" .RS 4 .nf void greet(read string name) { print($"hello, {name}") } void main() { var said = greet("Ada") print(said) } .fi .RE .PP .RS 4 .nf error: this call does not return a value --> example.sg:6:16 6 | var said = greet("Ada") | ^^^^^^^^^^^^ hint: call it on a line of its own: `greet("Ada")` .fi .RE .SS "A void call where a value is needed" .RS 4 .nf void greet(read string name) { print($"hello, {name}") } void main() { print(greet("Ada")) } .fi .RE .PP .RS 4 .nf error: this call does not return a value, and one is needed here --> example.sg:6:11 6 | print(greet("Ada")) | ^^^^^^^^^^^^ .fi .RE .SS "Dropping what is not there" .RS 4 .nf void greet(read string name) { print($"hello, {name}") } void main() { _ = greet("Ada") } .fi .RE .PP .RS 4 .nf error: this call does not return a value, so there is nothing to discard --> example.sg:6:9 6 | _ = greet("Ada") | ^^^^^^^^^^^^ hint: write the call alone, without `_ =` .fi .RE .SS "A list of void" .RS 4 .nf void main() { var nothing = List() print(nothing.length()) } .fi .RE .PP .RS 4 .nf error: `void` is no type of a value it is written as the return type of a function that gives nothing, in `Result` and `Task`, and as `raw void`, an address of memory of no known type --> example.sg:2:24 2 | var nothing = List() | ^^^^ .fi .RE .PP A variable, a parameter and a field of type \fBvoid\fR are refused the same way. .SS "void as the parameters" .RS 4 .nf int answer(void) { return 42 } void main() { print(answer()) } .fi .RE .PP .RS 4 .nf error: a function without parameters has nothing between its parentheses --> example.sg:1:12 1 | int answer(void) { | ^^^^ hint: `answer()` .fi .RE .PP A function with no parameters is written with empty parentheses: \fBint answer()\fR, not as C writes it. .SS "A main that returns a number" .RS 4 .nf int main() { return 0 } .fi .RE .PP .RS 4 .nf error: `main` is written `void main()` or `Result main()` to see the command line: `void main(List args)`; to await, `async` in front --> example.sg:1:5 1 | int main() { | ^^^^ .fi .RE .PP The code a program ends with is given by \fBexit(code)\fR, or by an error \fBmain\fR returns. .SH "SEE ALSO" .BR sg-return (7), .BR sg-async (7), .BR sg-raw (7), .BR sg-extern (7), .BR sg-export (7) .PP https://sg\-lang.org/reference/void.html