Skip to content

Commit 00df430

Browse files
jmcook1186cburgdorf
authored andcommitted
fix links to function pages
Signed-off-by: jmc <33655003+jmcook1186@users.noreply.github.com>
1 parent 0261c61 commit 00df430

9 files changed

Lines changed: 219 additions & 171 deletions

File tree

docs/src/SUMMARY.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
* [Comments](spec/comments.md)
2424
* [Items](spec/items/index.md)
2525
* [Visibility and Privacy](spec/items/visibility_and_privacy.md)
26-
* [Functions](spec/items/functions.md)
2726
* [Structs](spec/items/structs.md)
2827
* [Traits](spec/items/traits.md)
2928
* [Enums](spec/items/enums.md)

docs/src/spec/expressions/call.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ contract Foo {
3939
[expression]: ./index.md
4040
[IDENTIFIER]: ../lexical_structure/identifiers.md
4141
[INTEGER_LITERAL]: ../lexical_structure/tokens.md#integer-literals
42-
[function definition]: ../items/functions.md
42+
[function definition]: ../functions/index.md

docs/src/spec/functions/index.md

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,175 @@
1-
# Function calls
1+
# Functions
22

33
Constant size values stored on the stack or in memory can be passed into and returned by functions.
4+
5+
> **<sup>Syntax</sup>**\
6+
> _Function_ :\
7+
> &nbsp;&nbsp; _FunctionQualifiers_ `fn` [IDENTIFIER]\
8+
> &nbsp;&nbsp; &nbsp;&nbsp; `(` _FunctionParameters_<sup>?</sup> `)`\
9+
> &nbsp;&nbsp; &nbsp;&nbsp; _FunctionReturnType_<sup>?</sup>\
10+
> &nbsp;&nbsp; &nbsp;&nbsp; `{`\
11+
> &nbsp;&nbsp; &nbsp;&nbsp; _FunctionStatements_<sup>*</sup>\
12+
> &nbsp;&nbsp; &nbsp;&nbsp; `}`
13+
>
14+
> _FunctionQualifiers_ :\
15+
> &nbsp;&nbsp; `pub`<sup>?</sup>
16+
>
17+
> _FunctionStatements_ :\
18+
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_ReturnStatement_]\
19+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_VariableDeclarationStatement_]\
20+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_AssignStatement_]\
21+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_AugmentedAssignStatement_]\
22+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_ForStatement_]\
23+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_WhileStatement_]\
24+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_IfStatement_]\
25+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_AssertStatement_]\
26+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_BreakStatement_]\
27+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_ContinueStatement_]\
28+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_RevertStatement_]\
29+
> &nbsp;&nbsp; &nbsp;&nbsp; | [_Expression_]\
30+
>
31+
> _FunctionParameters_ :\
32+
> &nbsp;&nbsp; `self`<sup>?</sup> | `self,`<sup>?</sup> _FunctionParam_ (`,` _FunctionParam_)<sup>\*</sup> `,`<sup>?</sup>
33+
>
34+
> _FunctionParam_ :\
35+
> &nbsp;&nbsp; _FunctionParamLabel_<sup>?</sup> [IDENTIFIER] `:` [_Types_]
36+
>
37+
> _FunctionParamLabel_ :\
38+
> &nbsp;&nbsp; _ | [IDENTIFIER]
39+
>
40+
> _FunctionReturnType_ :\
41+
> &nbsp;&nbsp; `->` [_Types_]
42+
43+
44+
A _function_ definition consists of name and code block along with an optional
45+
list of parameters and return value. Functions are declared with the
46+
keyword `fn`. Functions may declare a set of *input* parameters,
47+
through which the caller passes arguments into the function, and
48+
the *output* [*type*][_Types_] of the value the function will return to its caller
49+
on completion.
50+
51+
When referred to, a _function_ yields a first-class *value* of the
52+
corresponding zero-sized [*function type*][_FunctionTypes_], which
53+
when called evaluates to a direct call to the function.
54+
55+
A function header prepends a set or curly brackets `{...}` which contain the function body.
56+
57+
For example, this is a simple function:
58+
59+
```fe
60+
fn add(x: u256, y: u256) -> u256 {
61+
return x + y
62+
}
63+
```
64+
65+
Functions can be defined inside of a contract, inside of a struct, or at the
66+
"top level" of a module (that is, not nested within another item).
67+
68+
Example:
69+
70+
```fe
71+
fn add(_ x: u256, _ y: u256) -> u256 {
72+
return x + y
73+
}
74+
75+
contract CoolCoin {
76+
balance: Map<address, u256>
77+
78+
fn transfer(mut self, from sender: address, to recipient: address, value: u256) -> bool {
79+
if self.balance[sender] < value {
80+
return false
81+
}
82+
self.balance[sender] -= value
83+
self.balance[recipient] += value
84+
return true
85+
}
86+
pub fn demo(mut self) {
87+
let ann: address = 0xaa
88+
let bob: address = 0xbb
89+
self.balance[ann] = 100
90+
91+
let bonus: u256 = 2
92+
let value: u256 = add(10, bonus)
93+
let ok: bool = self.transfer(from: ann, to: bob, value)
94+
}
95+
}
96+
```
97+
98+
Function parameters have optional labels. When a function is called, the
99+
arguments must be labeled and provided in the order specified in the
100+
function definition.
101+
102+
The label of a parameter defaults to the parameter name; a different label
103+
can be specified by adding an explicit label prior to the parameter name.
104+
For example:
105+
```fe
106+
fn encrypt(msg cleartext: u256, key: u256) -> u256 {
107+
return cleartext ^ key
108+
}
109+
110+
fn demo() {
111+
let out: u256 = encrypt(msg: 0xdecafbad, key: 0xfefefefe)
112+
}
113+
```
114+
115+
Here, the first parameter of the `encrypt` function has the label `msg`,
116+
which is used when calling the function, while the parameter name is
117+
`cleartext`, which is used inside the function body. The parameter name
118+
is an implementation detail of the function, and can be changed without
119+
modifying any function calls, as long as the label remains the same.
120+
121+
When calling a function, a label can be omitted when the argument is
122+
a variable with a name that matches the parameter label. Example:
123+
124+
```fe,ignore
125+
let msg: u256 = 0xdecafbad
126+
let cyf: u256 = encrypt(msg, key: 0x1234)
127+
```
128+
129+
A parameter can also be specified to have no label, by using `_` in place of a
130+
label in the function definition. In this case, when calling the function, the
131+
corresponding argument must not be labeled. Example:
132+
133+
```fe
134+
fn add(_ x: u256, _ y: u256) -> u256 {
135+
return x + y
136+
}
137+
fn demo() {
138+
let sum: u256 = add(16, 32)
139+
}
140+
```
141+
142+
Functions defined inside of a contract or struct may take [`self`][SELF] as a
143+
parameter. This gives the function the ability to read and write contract
144+
storage or struct fields, respectively. If a function takes `self`
145+
as a parameter, the function must be called via `self`. For example:
146+
147+
```fe,ignore
148+
let ok: bool = self.transfer(from, to, value)
149+
```
150+
`self` is expected to come *first* parameter in the function's parameter list.
151+
152+
153+
Functions can also take a [`Context`][CONTEXT] object which gives access to EVM features that read or write
154+
blockchain and transaction data. `Context` is expected to be *first* in the function's parameter list
155+
unless the function takes `self`, in which case `Context` should come second.
156+
157+
158+
[NEWLINE]: ../lexical_structure/tokens.md#newline
159+
[IDENTIFIER]: ../lexical_structure/identifiers.md
160+
[_Types_]: ../type_system/types/index.md
161+
[_FunctionTypes_]: ../type_system/types/function.md
162+
[SELF]: ./self.md
163+
[CONTEXT]: ./context.md
164+
[_ReturnStatement_]: ../statements/return.md
165+
[_VariableDeclarationStatement_]: ../statements/let.md
166+
[_AssignStatement_]: ../statements/assign.md
167+
[_AugmentedAssignStatement_]: ../statements/augassign.md
168+
[_ForStatement_]: ../statements/for.md
169+
[_WhileStatement_]: ../statements/for.md
170+
[_IfStatement_]: ../statements/if.md
171+
[_AssertStatement_]: ../statements/assert.md
172+
[_BreakStatement_]: ../statements/break.md
173+
[_ContinueStatement_]: ../statements/continue.md
174+
[_RevertStatement_]: ../statements/revert.md
175+
[_Expression_]: ../expressions/index.md

docs/src/spec/functions/self.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Self
2+
3+
`self` is used to represent the specific instance of a Contract. It is used to access variables that are owned by that specific instance. This works the same way for Fe contracts as for, e.g. `self` in the context of classes in Python, or `this` in Javascript. `self` gives access to constants from the contract code and state variables from contract storage.
4+
5+
> Note: Here we focus on functions defined inside a contract, giving access to contract storage; however, `self` can also be used to read and write `struct` fields where functions are defined inside `structs`.
6+
7+
If a function takes self as a parameter, the function must be called via self. For example:
8+
9+
```rust
10+
let ok: bool = self.transfer(from, to, value)
11+
```
12+
13+
## Mutability
14+
15+
`self` is immutable and can be used for read-only operations on the contract storage (or `struct` fields). In order to write to the contract storage, you must use `mut self`. This makes the contract instance mutable and allows the contract storage to be updated.
16+
17+
## Examples
18+
19+
### Reading contract storage
20+
21+
```fe
22+
contract example {
23+
24+
value: u256;
25+
26+
pub fn check_value(self) -> u256 {
27+
return self.value;
28+
}
29+
}
30+
```
31+
32+
### Writing contract storage
33+
34+
```fe
35+
contract example {
36+
37+
value: u256;
38+
39+
pub fn update_value(mut self) {
40+
self.value += 1;
41+
}
42+
}
43+
```

docs/src/spec/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* [Comments](comments.md)
1313
* [Items](items/index.md)
1414
* [Visibility and Privacy](items/visibility_and_privacy.md)
15-
* [Functions](items/functions.md)
1615
* [Structs](items/structs.md)
1716
* [Traits](items/traits.md)
1817
* [Enums](items/enums.md)

docs/src/spec/items/enums.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ fn f() {
6363

6464
[NEWLINE]: ../lexical_structure/tokens.md#newline
6565
[_IDENTIFIER_]: ../lexical_structure/identifiers.md
66-
[_Function_]: ./functions.md
66+
[_Function_]: ../functions/index.md
6767
[_Type_]: ../type_system/types/index.md
6868
[Enum type]: ../type_system/types/enum.md

0 commit comments

Comments
 (0)