How to write your first Cairo function

How to write your first Cairo function

Blockchain development has an average monthly search on Google of 100-1k. Source(google ad words). That is a lot. This only means a lot of people are interested in blockchain technology.

Just like any technology out there. Blockchain technology is broad and has so many aspects to it. The question on your mind right now is how can I get started? I have had this same question ruminating in my mind for some time and I came across StarkNet and the Cairo programming language.

The Cairo programming language is your gateway to creating powerful and life-changing smart contracts on StarkNet.

Whether you are a seasoned developer or a newcomer like myself. This blog post will guide you through one of the crucial building blocks of any smart contract.

By the end of this article, you will understand what functions are and how to use them in the Cairo programming language.

Understanding functions in general

Functions are like laid down steps or instructions on how to do something. It tells the computer what to do.

If you are like me and you love coffee a coffee machine is a great example of functions. You give a coffee machine some coffee beans, and it grinds the beans, adds hot water, brews the coffee, and gives you a nice cup of coffee with that golden crema we all love.

Simple yea. I told you.

In programming, functions are essential because they can help you to organize code and increase its readability.

Setting up your Cairo Development environment

To start writing Cairo you will have to set up your development environment. You essentially need two (2) things to get this rolling.

  1. Any text editor of your choice. I use VsCode

  2. Scarb

For me, I have found the easiest way to get this done and manage versions is by using asdf.

You can find a link to install asdf for whatever OS you are on here or reference the Cairo book here

Basic Syntax of a Cairo function

Cairo functions are small pieces of code ( sometimes not so small ) that do a specific task. It consists of five (5) parts.

  1. fn Keyword: This is how you start a function. It tells Cairo you're about to define a function.The keyword fn has to be used anytime you are creating a new function.

  2. Name: This is just a name you give to your function to identify them.

  3. Parameters: These are inputs to your function. Think of them as the coffee beans we gave our coffee machine earlier.

  4. Return Type: This tells what kind of output your function will produce. In our case a great cup of coffee.

  5. Body: This is where you write the actual instructions—the steps the function will follow.

fn make_coffee(coffee_beans) cup_of_coffee -> {
    // steps
    1. Grind coffee.
    2. Heat water.
    3. Brew coffee.
    4. output a nice cup of coffee.
}

Let's bring this home let's say you want to write a function that adds two numbers for you.

fn add_numbers(a: felt, b: felt) -> felt {
    retun a + b;
}

Best Practices for writing Cairo functions

When writing Cairo functions, following some best practices can make your code easier to read and maintain. Here’s how:

  1. Naming Conventions: Choose clear, descriptive names for your functions and variables. The names should tell you what the function or variable does. For example, calculate_sum is better than just sum. This makes your code easier to understand for others (and for yourself when you come back to it later).

  2. Code Organization: Keep your code organized by breaking it into small, manageable functions. Each function should do one specific task. Trust me you do not want your function to do more than one specific thing.

  3. Comments: Use comments to explain what your code does. This is especially important for complex parts of the code. Good documentation helps others (and future you) understand how your functions work and why you made certain decisions.

Run your first Cairo function

We have journeyed into the land of Cairo functions explaining what functions are and what the syntax looks like. It is time to run your first Cairo function if this is your first time or your nth time writing function depending on what stage of experience you are in.

First, you have to create a project using the below command

scarb new first_function

This will spin up a project and you can start playing writing your functions. Let's build a simple calculator.

For now, we are going to implement two (2) of the most basic arithmetic operations.

fn add_num(a: felt,  b: felt ) -> felt { // add two numbers
    return a + b ;
}

fn subtract_num(a: felt, b: felt) -> felt { // subtract two numbers
    return a - b; 
}

Your code will look like below

fn main() {

    let add = add_num(4, 4);
    println!("The addition of both numbers are: {}", add);

    let subtract =  subtract_num(10,5);

    println!("The subtration of both numbers are: {}", subtract);


}
fn add_num(a:u16,  b:u16 ) -> u16 { // add two numbers
    return a + b ;
}

fn subtract_num(a: u16, b: u16) -> u16 { // subtract two numbers
    return a - b ;
}

If all goes well when you run your Cairo program using scarb cairo run command you should get the result printed out in your terminal like so.

Conclusion

In this guide, we covered the basics of writing functions in Cairo programming from what functions are, to syntax, to setting up your development environment to running your first Cairo function using scarb cairo run.

One way to improve your skills is by practicing and practicing. Go ahead you do not have to be afraid to write some more complex functions and see if you can get them working on the first try.

Feel free to share them with me.

To dive deep into function in Cairo the Cairo book is a wonderful resource. Honestly, it is my goto