-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add about * add introduction * add links * update config.json * fix lint issues
- Loading branch information
Showing
4 changed files
with
128 additions
and
5 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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
{ | ||
"blurb": "<todo>", | ||
"authors": [ | ||
"<your_gh_username>" | ||
], | ||
"blurb": "Functions in Cairo let you organize and reuse code by defining reusable blocks with a name, parameters, and an optional return value.", | ||
"authors": ["0xNeshi"], | ||
"contributors": [] | ||
} |
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 |
---|---|---|
@@ -1 +1,117 @@ | ||
# Functions | ||
|
||
Functions are fundamental in Cairo. | ||
|
||
By convention, Cairo uses **snake case** for function and variable names, where words are lowercase and separated by underscores. | ||
|
||
Here's an example: | ||
|
||
```rust | ||
fn another_function() { | ||
println!("Another function."); | ||
} | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
another_function(); | ||
} | ||
``` | ||
|
||
Functions are defined with `fn`, followed by a name, parentheses for parameters (if any), and a body enclosed in curly brackets. | ||
|
||
You can call a function by its name followed by parentheses. | ||
|
||
Function order doesn't matter as long as they're in scope. | ||
|
||
## Parameters | ||
|
||
Functions can take **parameters**, which are variables defined in the function signature. | ||
|
||
When calling the function, you provide **arguments**, the values passed to these parameters. | ||
|
||
```rust | ||
fn main() { | ||
another_function(5); | ||
} | ||
|
||
fn another_function(x: felt252) { | ||
println!("The value of x is: {}", x); | ||
} | ||
``` | ||
|
||
Here, `x` is a parameter of type `felt252`. | ||
|
||
When called with `5`, the function prints "The value of x is: 5". | ||
|
||
Multiple parameters are separated by commas: | ||
|
||
```rust | ||
fn main() { | ||
print_labeled_measurement(5, "h"); | ||
} | ||
|
||
fn print_labeled_measurement(value: u128, unit_label: ByteArray) { | ||
println!("The measurement is: {value}{unit_label}"); | ||
} | ||
``` | ||
|
||
This prints: `The measurement is: 5h`. | ||
|
||
## Statements and Expressions | ||
|
||
Cairo functions are made up of **statements** and **expressions**: | ||
|
||
- **Statements** perform actions but do not return values. | ||
For example, `let x = 3;` is a statement. | ||
- **Expressions** evaluate to a value. | ||
For example, `5 + 6` evaluates to `11`. | ||
|
||
Blocks enclosed in curly brackets are also expressions, and their final value is returned: | ||
|
||
```rust | ||
fn main() { | ||
let y = { | ||
let x = 3; | ||
x + 1 | ||
}; | ||
println!("The value of y is: {}", y); | ||
} | ||
``` | ||
|
||
Here, `y` is assigned the value `4`. | ||
|
||
## Return Values | ||
|
||
Functions can return values using the `->` syntax to specify the return type. | ||
|
||
By default, a function returns the value of its final expression: | ||
|
||
```rust | ||
fn five() -> u32 { | ||
5 | ||
} | ||
|
||
fn main() { | ||
let x = five(); | ||
println!("The value of x is: {}", x); | ||
} | ||
``` | ||
|
||
This outputs: `The value of x is: 5`. | ||
|
||
For more complex cases: | ||
|
||
```rust | ||
fn plus_one(x: u32) -> u32 { | ||
x + 1 | ||
} | ||
|
||
fn main() { | ||
let x = plus_one(5); | ||
println!("The value of x is: {}", x); | ||
} | ||
``` | ||
|
||
This prints: `The value of x is: 6`. | ||
|
||
Be mindful not to add a semicolon at the end of the final expression, as it would turn the expression into a statement, causing the function to return `()` (unit type) instead of the expected value. |
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
# Introduction | ||
|
||
Functions in Cairo let you organize and reuse code by defining reusable blocks with a name, parameters, and an optional return value. | ||
They consist of statements and expressions, where the latter can produce a value. | ||
Learning how to define, call, and work with parameters and return values is key to writing clean and efficient Cairo programs. |
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 |
---|---|---|
@@ -1 +1,6 @@ | ||
[] | ||
[ | ||
{ | ||
"url": "https://book.cairo-lang.org/ch02-03-functions.html", | ||
"description": "Functions in The Cairo book" | ||
} | ||
] |