Skip to content

Commit

Permalink
Functions concept (#324)
Browse files Browse the repository at this point in the history
* add about

* add introduction

* add links

* update config.json

* fix lint issues
  • Loading branch information
0xNeshi authored Dec 24, 2024
1 parent 74836be commit 66e0ec2
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 5 deletions.
6 changes: 2 additions & 4 deletions concepts/functions/.meta/config.json
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": []
}
116 changes: 116 additions & 0 deletions concepts/functions/about.md
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.
4 changes: 4 additions & 0 deletions concepts/functions/introduction.md
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.
7 changes: 6 additions & 1 deletion concepts/functions/links.json
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"
}
]

0 comments on commit 66e0ec2

Please sign in to comment.