Skip to content

Commit

Permalink
feat: function attributes (#34)
Browse files Browse the repository at this point in the history
* feat: add function attributes chapter

* fix: func attributes, missing details, renaming

* fix: missing '_' in function name
  • Loading branch information
julio4 authored Jun 20, 2023
1 parent 8373d82 commit f4b6ebb
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions listings/ch00-introduction/function_attributes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
10 changes: 10 additions & 0 deletions listings/ch00-introduction/function_attributes/Scarb.toml
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]]
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()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod function_attributes;
Empty file modified scripts/cairo_programs_verifier.sh
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Summary
- [Storing Arrays](./ch00-05-storing_arrays.md)
- [Mappings](./ch00-06-mappings.md)
- [Constructors](./ch00-07-constructor.md)
- [Function Attributes](./ch00-08-function_attributes.md)

- [Applications](./ch01-00-applications.md)
- [Upgradeable Contract](./ch01-01-upgradeable_contract.md)
17 changes: 17 additions & 0 deletions src/ch00-08-function_attributes.md
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}}
```

0 comments on commit f4b6ebb

Please sign in to comment.