Skip to content

Commit 270fe8f

Browse files
Changes to functions.md (#998)
* Update functions.md Signed-off-by: Neil Henderson <[email protected]> * Tweak comments for divide example --------- Signed-off-by: Neil Henderson <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
1 parent ecb0031 commit 270fe8f

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

docs/cpp2/functions.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
## Overview
55

6-
A function is defined by writing a function signature after the `:` and a statement (expression or `{` `}` compound statement) after the `=`. After the optional [template parameters](declarations.md#template-parameters) available for all declarations, a function signatures consists of a possibly-empty [parameter list](#parameters), and an optional function [return values](#return-values).
6+
A function is defined by writing a function signature after the `:` and a statement (expression or `{` `}` compound statement) after the `=`. After the optional [template parameters](declarations.md#template-parameters) available for all declarations, a function signature consists of a possibly-empty [parameter list](#parameters), and one or more optional [return values](#return-values).
77

88
For example, the minimal function named `func` that takes no parameters and returns nothing (`#!cpp void`) is:
99

@@ -14,7 +14,7 @@ func: ( /* no parameters */ ) = { /* empty body */ }
1414
1515
## <a id="parameters"></a> Parameters
1616
17-
The parameter list is enclosed by `(` `)` parentheses, and the parameters separated by commas. Each parameter is declared using the [same syntax as any object](declarations.md). For example:
17+
The parameter list is enclosed by `(` `)` parentheses and the parameters are separated by commas. Each parameter is declared using the [same unified syntax](declarations.md) as used for all declarations. For example:
1818
1919
``` cpp title="Declaring parameters" hl_lines="2-4"
2020
func: (
@@ -67,7 +67,29 @@ add: (a, b) -> _ = a+b;
6767
6868
(2) **`#!cpp -> ( /* parameter list */ )`** to return a list of named return parameters using the same [parameters](#parameters) syntax, but where the only passing styles are `out` (the default, which moves where possible) or `forward`. The function body must [initialize](objects.md#init) the value of each return-parameter `ret` in its body the same way as any other local variable. An explicit return statement is written just `#!cpp return;` and returns the named values; the function has an implicit `#!cpp return;` at the end. For example:
6969
70-
``` cpp title="Functions with multiple/named return values" hl_lines="7 9 10 22-24"
70+
``` cpp title="Function with multiple/named return values" hl_lines="1 3-4 7-8 14 16-17"
71+
divide: (dividend: int, divisor: int) -> (quotient: int, remainder: int) = {
72+
if divisor == 0 {
73+
quotient = 0; // constructs quotient
74+
remainder = 0; // constructs remainder
75+
}
76+
else {
77+
quotient = dividend / divisor; // constructs quotient
78+
remainder = dividend % divisor; // constructs remainder
79+
}
80+
}
81+
82+
main: () = {
83+
div := divide(11, 5);
84+
std::cout << "(div.quotient)$, (div.remainder)$\n";
85+
}
86+
// Prints:
87+
// 2, 1
88+
```
89+
90+
This next example declares a [member function](types.md#this-parameter) with multiple return values in a [type](types.md) named `set`:
91+
92+
``` cpp title="Member function with multiple/named return values" hl_lines="7 9 10 22-24"
7193
set: <Key> type = {
7294
container: std::set<Key>;
7395
iterator : type == std::set<Key>::iterator;

0 commit comments

Comments
 (0)