Variables in Mojo – Explained with Visuals

variables in mojo

If you are new to Mojo programming by Modular, variables are one of the first things you should learn. Variables are important in every programming language. They help you store and work with data. This guide will explain the basics of variables in Mojo.

What is a Variable in Mojo?

A variable is like a labelled basket that tells you what it holds. For example, you might have a basket labelled “Apples” that contains apples. In Mojo, a variable is a named memory location in RAM, having a name and a type.

variable in mojo example
Variable in MOJO

Type Annotations in Mojo

Type annotations in Mojo allow you to specify the type of a variable when you declare it. This ensures the variable holds a specific type of data. or example, you can write let height: Int = 170, which means age is explicitly an integer, or let name: String = "Hasnain", which means name is explicitly a string. Using type annotations makes the code clearer and helps prevent errors by enforcing the correct data type.

Syntax

var variable_name: type = value

Example

let height: Int = 170
let name: String = "Hasnain"

Declaring and Initializing Variables in MOJO

In Mojo, you can create and set up variables in two main ways: implicitly or explicitly. When you use implicit typing, Mojo guesses the type of the variable based on the value you give it. In explicit typing, you clearly state what type the variable is. Knowing both ways is important to write simple and efficient code.

Implicitly-Declared Variables

When you create a variable implicitly, Mojo figures out the type of the variable based on the value you give it. This makes the code simpler because you don’t have to state the type, making it shorter and easier to read.

For example, in the image below, Person A says, “My height is 170,” but doesn’t explain if it’s in centimetres, meters, or something else. Person B assumes it’s in centimetres because of the situation, which makes it implicit. This is like how Mojo handles implicit variables. If you write height = 170, Mojo guesses the type of the variable (like “integer”) based on the value you give it.

Explaining the implicit declared variable with visual.
Implicit Declared Variable

Syntax

variable_name = value
  • variable_name: The name you give to your variable.
  • value: The data or value you want to store in the variable.

Example

height = 170

Explicitly-Declared Variables

Unlike implicit declaration, explicitly declaring a variable means you have to state the variable type clearly. This is helpful when you want to make sure the variable only holds a certain type of value. It also makes your code clearer and safer.

For example, in the image below, Person A says, “My height is 170,” and Person B asks, “Is that in centimetres, meters, or feet?” It happened because Person A didn’t say which unit they meant. In the same way, when you declare a variable explicitly in Mojo, you clearly tell the program what type of value the variable holds. For example, writing var height: Int = 170 means you are saying that height is an integer. Similarly, writing var name: String = "Hasnain" makes it clear that name is a string. This removes any confusion and makes everything clear and easy to understand, just like giving the exact unit in the conversation.

Explaining the explicit declared variable with visual.
Explicit Declared Variable

Syntax

var variable_name: type = value
  • variable_name: The name you give to your variable.
  • type: The data type example Int, String.
  • value: The data or value you want to store in the variable.

Example

var height: Int = 170
var name: String = "Hasnain"

Updating Variables in Mojo

When you define a variable for the first time, you assign it a value. Later, to modify the value the variable holds, you simply assign a new value to it. For example, the variable height starts with a value of 170. Later, when you write height = 180, Mojo updates the value of height to 180.

Syntax

variable_name = new_value
  • variable_name: The name you give to your variable.
  • new_value: The new data or value you want to store in the variable.

Example

height = 170	# First, we set height to 170
height = 180	# Now, we update height to 180

On the left side, height is assigned the value 170, meaning it points to a memory location that holds 170 (height = 170). On the right side, height is updated to 180, so it now points to a new memory location that holds 180 (height = 180).

Update a variable in mojo
Updating variable in MOJO

Variable Scopes

Variable scope is about where you can use or change a variable in your code. It decides where a variable can be accessed and how long it lasts.

Lexical Scoping with var

Lexical scoping means a variable can only be accessed within the part of the code where it’s created, and in any blocks inside it. The var keyword creates a variable that can be used only within the block or scope it’s defined, such as inside an if block. Shadowing occurs when a variable inside a block has the same name as an outer variable, causing the inner variable to hide or overwrite the outer one.

Example

In the example, the outer num is 5, and inside the if block, a new num is declared using var, which shadows the outer one. The inner num is 10 within the block, but the outer num remains 5 after the block ends, demonstrating lexical scoping and variable shadowing.

num = 5  # Outer variable

if num == 5:
    var num = 10  # Inner variable shadows the outer num
    print(num)  # Prints: 10 (Inner num)

print(num)  # Prints: 5 (Outer num remains unchanged)

Function-Level Scoping

Function-level scoping means that variables created inside a function can be used anywhere within that function, even in inner blocks like if or for loops. If you change a variable inside a block, the change will affect the whole function, not just that block. This occurs for variables that are declared without using var.

Example

The variable num is modified inside the if block. Since Mojo uses function-level scoping, the change to num affects the whole function, so the print(num) outside the block shows 10 instead of 5.

num = 5  # Variable declared outside any block

if num == 5:
    num = 10  # Change the num inside the block

print(num)  # Prints: 10 (Change affects the entire function)

Conclusion

In Mojo, variables can be declared and initialized implicitly or explicitly. Variables also follow scoping rules: implicit variables are function-scoped, while explicit ones are lexical-scoped. Understanding how to declare, update, and manage variable scope ensures clear, efficient, and error-free programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top