Functions are widely used in the Cairo programming language. You should have already seen the function main , which is the starting point of many programs. We assume that a function is defined by the keyword fn and that you are familiar with the basic structure of a function from the previous sections.
In Cairo, functions are named in snake_case format. Here is an example:
use debug::PrintTrait; fn another_function() { 'Another function.'.print(); } fn main() { 'Hello, world!'.print(); another_function(); }
In the code above, we defined the function another_function before the function main , but we could have defined it later. Cairo doesn't care where your functions are defined, as long as they are accessible from wherever they are called.
When we run this program with the scarb cairo-run command, we get the following output: [DEBUG] Hello, world! (raw: 5735816763073854953388147237921) [DEBUG] Another function. (raw: 22265147635379277118623944509513687592494)
Parameters
Parameters are variables used to get the values required for the function to work. The parenthesized parameter list of a function ensures that these values are passed when the function is called. Here is an example of adding parameters to the function another_function another_function:
use debug::PrintTrait; fn main() { another_function(5, 6); } fn another_function(x: felt252, y: felt252) { x.print(); y.print(); }
In this example, we have created a function called another_function and added two parameters to it. The first parameter is called x, and the second is called y and will be defined by default as felt252 since the data types are not specified. The function then prints the values x and y to the terminal.
To run the code, you can use the scarb cairo-run command in the terminal.
Named Parameters
Named parameters allow you to specify the names of arguments when calling a function. If you want to use named parameters, you must specify the name of the parameter and the value you want to pass. Note that they must have the same order as specified in the function.
fn foo(x: u8, y: u8) { // ... } fn main() { let first_arg = 3; let second_arg = 4; // parametre_adı: değeri foo(x: first_arg, y: second_arg); // foo(y: second_arg, x: first_arg); <- bu şekil bir hata üretecektir }
Statements and Expressions
Function bodies consist of a sequence of statements and optionally terminate with an Expression. The functions we have covered so far do not contain a terminating Expression, but you have seen an Expression as part of a Statement. Cairo is an Expression-based language, so it is important to understand this difference.
Statements: Instructions that perform an action and do not return a value. For example
let y = 6;
This is a Statement.
Expressions: Pieces of code that are resolved to a value.
In Cairo, a Statement (for example let y = 6;) returns no value, therefore:
let x = (let y = 6);
such code generates an error. Expressions, however, can return values and be part of a Statement:
let y = { let x = 3; x + 1 };
This Expression resolves to 4 in this case.
If you add a semicolon at the end of Expression, you make it a Statement and it no longer returns a value. This is a concept you should take into account when dealing with function return values.
Functions with Return Value
Functions with a return value in Cairo calculate a value and return it to the caller. At the end of the function return , the value to be returned is specified. Such functions are used to return the result after completing the calculations and this value can be used by the caller.
We do not name the return values, but we must declare their type after an arrow (->). Below is an example of this:
use debug::PrintTrait; fn main() { let x = plus_one(5); x.print(); } fn plus_one(x: u32) -> u32 { x + 1 }
When you run this code [DEBUG] (raw: 6) you get the output. However, if you add a semicolon at the end of the expression x + 1 it will stop being an expression and you will get an error like the one below:
error: Unexpected return type. Expected: "core::integer::u32", found: "()"