.\" Made by website/plain/build.mjs from website/words/return.md; change that file. .TH SG-RETURN 7 2026-11-28 "SG" "SG reference" .SH NAME return \- ends the function, with its value .SH SYNOPSIS .TP \fBreturn value\fR ends the function and gives the value to the caller .TP \fBreturn\fR ends a void function .TP \fBreturn Ok(value), return Error(e), return Ok()\fR ends a function that returns a Result .TP \fBreturn null\fR ends a function that returns a T?, with no value .TP \fBreturn x, with read T as the return type\fR gives a borrow of what a parameter holds .TP \fBx ?? return value\fR the value of x, or a return .TP \fBP => return value\fR an arm of a match that leaves the function .SH DESCRIPTION \fBreturn\fR ends the function at once, wherever it stands: in a loop, in an \fBif\fR, in an arm of a \fBmatch\fR\&. The value after it must have the type the function returns. A \fBvoid\fR function ends at its last line, or earlier with a bare \fBreturn\fR, and a \fBreturn\fR with a value there is an error. .PP A function that returns a value must reach a \fBreturn\fR on every path, and the compiler checks it. A function that ends with a loop no \fBbreak\fR leaves (\fBwhile true\fR) needs no \fBreturn\fR after it. .PP The value is moved to the caller, not copied: a list built in the function goes to the caller as it is. What else the function owns is let go on the way out. .PP A function can give back a borrow, written \fBread T\fR or \fBref T\fR in its return type. The borrow must come from a parameter, never from a variable of the function, which is gone once it returns. When the function borrows more than one parameter, \fBfrom\fR after them says which the result can come from. .PP \fBreturn\fR is a statement. Inside an expression it is written only on the right of \fB??\fR and as an arm of a \fBmatch\fR; an arm of \fB?:\fR cannot leave the function. .PP \fBmain\fR returns nothing, \fBvoid main()\fR, or a \fBResult\fR: an error it returns is printed after \fBerror: \fR and the program exits with code 1. \fBexit(code)\fR ends the program with any other code. .SH EXAMPLES .SS "A value, and leaving early" .RS 4 .nf int square(int x) { return x * x } void greet(read string name) { if name.length() == 0 { return } print($"hello, {name}") } void main() { print(square(7)) greet("") greet("Pasha") } .fi .RE .PP .RS 4 .nf 49 hello, Pasha .fi .RE .PP The bare \fBreturn\fR ends \fBgreet\fR before it prints, so the empty name prints nothing. .SS "From inside a loop" .RS 4 .nf string? firstLong(read List words) { for w in read words { if w.length() > 4 { return w.clone() } } return null } void main() { var animals = List { "cat", "lion", "tiger" } print(firstLong(read animals) ?? "none") var short = List { "ox" } print(firstLong(read short) ?? "none") } .fi .RE .PP .RS 4 .nf tiger none .fi .RE .PP The \fBreturn\fR in the loop ends the function at the first long word. When the loop ends without one, the \fBreturn null\fR below it runs. .SS "The value of a match" .RS 4 .nf string grade(int score) { return match score { 90..=100 => "A" 75..=89 => "B" _ => "C" } } void main() { print(grade(95)) print(grade(80)) print(grade(12)) } .fi .RE .PP .RS 4 .nf A B C .fi .RE .PP The whole \fBmatch\fR is the value returned. .SS "Leaving in the middle of a line" .RS 4 .nf int discount(read string code) { var percent = match code { "SPRING" => 10 "VIP" => 25 _ => return 0 } print($"{code}: {percent}%") return percent } int portOf(read string text) { var port = int.parse(text) ?? return 80 return port } void main() { print(discount("VIP")) print(discount("FREE")) print(portOf("8080")) print(portOf("http")) } .fi .RE .PP .RS 4 .nf VIP: 25% 25 0 8080 80 .fi .RE .PP An unknown code takes the last arm, which returns 0 and prints nothing. \fB?? return 80\fR gives the number when the text is one, and returns 80 when it is not. These are the two places a \fBreturn\fR goes inside a line. .SS "Giving the value to the caller" .RS 4 .nf List evens(int count) { var result = List() var n = 0 while result.length() < count { result.add(n) n += 2 } return result } void main() { var xs = evens(4) xs.add(100) print(xs) } .fi .RE .PP .RS 4 .nf [0, 2, 4, 6, 100] .fi .RE .PP \fBreturn result\fR moves the list to the caller: nothing is copied, and \fBxs\fR owns it from then on. .SS "A borrow" .RS 4 .nf read string longest(read List names) { nint best = 0 for i in 0..names.length() { if names[i].length() > names[best].length() { best = i } } return names[best] } read string longer(read string a, read string b) from a, b { if a.length() >= b.length() { return a } return b } void main() { var names = List { "Ada", "Grace", "Linus" } print(longest(read names)) print(longer("Ada", "Grace")) } .fi .RE .PP .RS 4 .nf Grace Grace .fi .RE .PP \fBlongest\fR gives back a borrow of an item of the list, not a copy of the text. \fBlonger\fR borrows two parameters, so \fBfrom a, b\fR says the result can come from either; the caller keeps both lent while it uses the result. .SS "From main" The file check/main.sg: .PP .RS 4 .nf Result main(List args) { if args.length() == 0 { return Error("give a file name".clone()) } print($"checking {args[0]}") return Ok() } .fi .RE .PP .RS 4 .nf $ cd check $ sg main.sg; echo "exit code $?" error: give a file name exit code 1 $ sg main.sg notes.txt; echo "exit code $?" checking notes.txt exit code 0 .fi .RE .PP The error \fBmain\fR returns is printed, and the program ends with code 1. \fBreturn Ok()\fR ends it with code 0. .SH "WHAT THE COMPILER REFUSES" .SS "A path with no return" .RS 4 .nf string size(int n) { if n < 10 { return "small" } else if n < 100 { return "medium" } } void main() { print(size(5)) } .fi .RE .PP .RS 4 .nf error: `size` does not return a value on every path; it must return `string` --> example.sg:1:8 1 | string size(int n) { | ^^^^ .fi .RE .PP A number of 100 or more reaches the end with nothing to give. An \fBelse { return "large" }\fR makes it whole. .SS "A line after return" .RS 4 .nf int f() { return 1 print("never") } void main() { print(f()) } .fi .RE .PP .RS 4 .nf error: this line is never run: the `return` before it leaves the function --> example.sg:3:5 3 | print("never") | ^^^^^^^^^^^^^^ hint: take it away, with the lines after it in the block .fi .RE .PP There are no warnings, so a line that can never run is an error, as an arm of a \fBmatch\fR that never runs is. The same holds after \fBbreak\fR, \fBcontinue\fR, \fBpanic(...)\fR and \fBexit(...)\fR, after an \fBif\fR whose branches all leave, and after a \fBwhile true\fR that no \fBbreak\fR leaves. .SS "A bare return where a value is due" .RS 4 .nf Result save() { return } void main() { _ = save() } .fi .RE .PP .RS 4 .nf error: `save` must return a `Result` value --> example.sg:2:5 2 | return | ^^^^^^ hint: success is written too: `return Ok()` .fi .RE .SS "A value from a void function" .RS 4 .nf void greet(read string name) { print(name) return 0 } void main() { greet("Ada") } .fi .RE .PP .RS 4 .nf error: `greet` is void and cannot return a value --> example.sg:3:12 3 | return 0 | ^ .fi .RE .SS "A borrow of a variable of the function" .RS 4 .nf read string made() { var text = "Pasha".clone() return read text } void main() { print(made()) } .fi .RE .PP .RS 4 .nf error: this returns a borrow of `text`, and `text` is gone when the function returns --> example.sg:3:12 3 | return read text | ^^^^^^^^^ hint: return the value itself instead of a borrow of it .fi .RE .PP Written \fBstring made()\fR with \fBreturn text\fR, the function gives the text itself to the caller. Written \fBreturn text\fR in \fBread string made()\fR, the message says the same before anything is lent: .PP .RS 4 .nf read string made() { var text = "Pasha".clone() return text } void main() { print(made()) } .fi .RE .PP .RS 4 .nf error: expected `read string`, found `string` `text` is a variable of the function, gone when it returns: a borrow of it would outlive it --> example.sg:3:12 3 | return text | ^^^^ hint: return the value itself: `string made(...)` .fi .RE .SS "A borrow of two parameters, with no from" .RS 4 .nf read string longer(read string a, read string b) { if a.length() >= b.length() { return a } return b } void main() { print(longer("Ada", "Grace")) } .fi .RE .PP .RS 4 .nf error: `longer` returns a borrow and borrows 2 parameters, so it says which of them the result comes from --> example.sg:1:13 1 | read string longer(read string a, read string b) { | ^^^^^^ hint: after the parameters: `from a, b`, or only the ones it can come from .fi .RE .SS "return in an arm of ?:" .RS 4 .nf int pick(bool b) { var x = b ? 1 : return 0 return x } void main() { print(pick(true)) } .fi .RE .PP .RS 4 .nf error: an arm of `?:` gives a value; it cannot leave the function --> example.sg:2:21 2 | var x = b ? 1 : return 0 | ^^^^^^ hint: leave with an `if`, or with `?? return` on a value that may be `null` .fi .RE .SS "return inside another expression" .RS 4 .nf void show(int n) { print(n) } int f(int n) { show(return 2) return n } void main() { print(f(1)) } .fi .RE .PP .RS 4 .nf error: expected an expression, found `return` inside an expression `return` is written only on the right side of `??` or as an arm of a `match` --> example.sg:6:10 6 | show(return 2) | ^^^^^^ .fi .RE .SS "A main that returns a number" .RS 4 .nf int main() { return 3 } .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(3)\fR, or by a \fBResult\fR that \fBmain\fR returns. .SH "SEE ALSO" .BR sg-void (7), .BR sg-try (7), .BR sg-match (7), .BR sg-while (7), .BR sg-read (7) .PP https://sg\-lang.org/reference/return.html