-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add function attributes chapter * fix: func attributes, missing details, renaming * fix: missing '_' in function name
- Loading branch information
Showing
7 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "function_attributes" | ||
version = "0.1.0" | ||
|
||
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest | ||
|
||
[dependencies] | ||
starknet = "1.1.0" | ||
|
||
[[target.starknet-contract]] |
29 changes: 29 additions & 0 deletions
29
listings/ch00-introduction/function_attributes/src/function_attributes.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#[contract] | ||
mod FunctionAttributes { | ||
|
||
struct Storage { | ||
_value: u32 | ||
} | ||
|
||
// The `set` function is marked as external because it writes to storage (_value) | ||
// and can be called from outside the contract | ||
#[external] | ||
fn set(value: u32) { | ||
_value::write(value); | ||
} | ||
|
||
// The `get` function is marked as view because it doesn't write to storage | ||
// and can be called from outside the contrac | ||
#[view] | ||
fn get() -> u32 { | ||
// We can call an internal function from any functions within the contract | ||
_read_value() | ||
} | ||
|
||
// The `_read_value` function doesn't have any attributes, so it's an internal function | ||
// and can only be called from within the contract | ||
fn _read_value() -> u32 { | ||
_value::read() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod function_attributes; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Function Attributes | ||
|
||
When you write functions in a contract using Cairo, you need to provide special annotations to inform the compiler about the nature of each function. These annotations are written as `#[attribute]` just before the function definition. | ||
|
||
Let's explore the three main types of functions you can use: | ||
|
||
- `#[external]`: These functions can be called from anywhere and have the ability to modify the contract's state. However, it's important to note that interacting with external functions requires sending a transaction, which incurs a gas cost. | ||
|
||
- `#[view]`: These functions can also be called from anywhere, but they are read-only and cannot modify the contract's state. The advantage of using view functions is that they can also be invoked without making a transaction. You can interact with them directly through a RPC node, making them essentially free to call! | ||
|
||
- Internal: These functions are only accessible within the contract itself and have the ability to modify the contract's state. They are private by default, unlike external and view functions. To mark a function as internal, you simply don't add any annotation to them. | ||
|
||
Let's take a look at a simple example contract to see these attributes in action: | ||
|
||
```rust | ||
{{#include ../listings/ch00-introduction/function_attributes/src/function_attributes.cairo}} | ||
``` |