From 6d2624bc912ae7ff49269287a59dce68e2b78498 Mon Sep 17 00:00:00 2001 From: Nenad Date: Mon, 23 Dec 2024 21:37:47 +0100 Subject: [PATCH 1/5] add about --- concepts/functions/about.md | 116 ++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/concepts/functions/about.md b/concepts/functions/about.md index 0c5faf50..0dce83cf 100644 --- a/concepts/functions/about.md +++ b/concepts/functions/about.md @@ -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. \ No newline at end of file From 057c9f85eda5cdf7084262a2bdbbfb85214078d4 Mon Sep 17 00:00:00 2001 From: Nenad Date: Mon, 23 Dec 2024 21:38:28 +0100 Subject: [PATCH 2/5] add introduction --- concepts/functions/introduction.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/concepts/functions/introduction.md b/concepts/functions/introduction.md index e10b99d0..12538fc8 100644 --- a/concepts/functions/introduction.md +++ b/concepts/functions/introduction.md @@ -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. From 060152f73740d437feaf11be78bdeb1117c3a513 Mon Sep 17 00:00:00 2001 From: Nenad Date: Mon, 23 Dec 2024 21:39:59 +0100 Subject: [PATCH 3/5] add links --- concepts/functions/links.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/concepts/functions/links.json b/concepts/functions/links.json index fe51488c..af2957b8 100644 --- a/concepts/functions/links.json +++ b/concepts/functions/links.json @@ -1 +1,6 @@ -[] +[ + { + "url": "https://book.cairo-lang.org/ch02-03-functions.html", + "description": "Functions in The Cairo book" + } +] From 7f6674cbcd2c4ccf96e7666bcd8be64aa1f031c6 Mon Sep 17 00:00:00 2001 From: Nenad Date: Mon, 23 Dec 2024 21:41:19 +0100 Subject: [PATCH 4/5] update config.json --- concepts/functions/.meta/config.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/concepts/functions/.meta/config.json b/concepts/functions/.meta/config.json index 35b1d32b..9d612d9b 100644 --- a/concepts/functions/.meta/config.json +++ b/concepts/functions/.meta/config.json @@ -1,7 +1,5 @@ { - "blurb": "", - "authors": [ - "" - ], + "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": [] } From 5f99594b8390afa2304c0111d65a60d05bdedb94 Mon Sep 17 00:00:00 2001 From: Nenad Date: Mon, 23 Dec 2024 21:42:32 +0100 Subject: [PATCH 5/5] fix lint issues --- concepts/functions/about.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/concepts/functions/about.md b/concepts/functions/about.md index 0dce83cf..01fe83ea 100644 --- a/concepts/functions/about.md +++ b/concepts/functions/about.md @@ -97,7 +97,7 @@ fn main() { } ``` -This outputs: `The value of x is: 5`. +This outputs: `The value of x is: 5`. For more complex cases: @@ -112,6 +112,6 @@ fn main() { } ``` -This prints: `The value of x is: 6`. +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. \ No newline at end of file +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.