Variables (Declared and Undeclared variables) in Mojo Programming Language — An Overview

Shriram Sivanandhan
3 min readMay 3, 2024

--

Mojo Programming Language combines the usability of Python with the performance of C, C++ and Rust. Mojo Programming Language utilizes next-generation compiler technologies with features like caching, multithreading and cloud distribution technologies. Further, Auto-Tuning and compile-time Metaprogramming features allows to write code for several hardware. The advantages of Mojo Programming Language are Usability & Programmability, best performance, Interoperability and Extensibility.

As we saw Interoperability of Mojo Programming Language with Python Ecosystem to use Matplotlib Python Visualization Library, Pandas Python Data Analysis and Manipulation Library, NumPy (Fundamental Package for Scientific Computing in Python) Library, SciPy Python Library in Mojo Programming Language and various concepts and implementations regarding Mojo Programming Language in previous Articles/Blogs. In this Article/Blog, we are going to see an overview on Variables (Declared and Undeclared variables) in Mojo Programming Language.

Since the older JupyterLab-based Mojo Playground is deprecated, in this Blog, Programming in Mojo is done using the new Mojo Playground.

Variables (Declared and Undeclared variables) in Mojo Programming Language:

A variable is a name that holds/stores a value or object. All the variables in Mojo Programming Language are mutable (i.e. their value can be changed).

Undeclared Variables:

We can create a variable with a name and a value associated to it without declaring it within a def Function or a REPL environment. We should be aware that Undeclared variables are not allowed in an fn Function or as a struct field.

Example Code 1:

Undeclared Variables in Mojo Programming Language (In New Mojo Playground)

In the above Example Code 1, we had just created a variable with name “a” and a value associated to it.

Declared variables:

We can declare a variable using the var Keyword as shown in the below Example Code 2,

Example Code 2:

Declared Variables in Mojo Programming Language (In New Mojo Playground)

Here in the Example Code 2, we can see that the variable “a” has been initialized to the string “Hello, Mojo” and the variable “b” has a type declared — Int (an Integer value), but it is uninitialized. All the Declared Variable are typed which is either by explicitly mentioning with a type annotation or by implicitly when the variable is initialized with a value.

Declared variables follow Lexical Scoping, where Undeclared variables do not follow Lexical Scoping. When we use Mojo Programming Language in a REPL environment, the Top-Level variables which are the variables outside a Function or struct do not require var declarations. Declared variables are strongly typed and hence we cannot assign value to a Declared variable of different type, unless those types can be implicitly converted. To explain this, see the below Example Code 3,

Example Code 3:

Declared Variables in Mojo Programming Language (In New Mojo Playground)

Actually, Mojo Programming Language formerly supported the let Keyword for Declaring variables that are Immutable, but this way of Declaring variables using let Keyword has been removed to simplify the Programming Language, and for other reasons mentioned here https://github.com/modularml/mojo/blob/main/proposals/remove-let-decls.md which is being mentioned by the Modular-Mojo Development Team here https://docs.modular.com/mojo/manual/variables.

We had seen an overview on Variables (Declared and Undeclared variables) in Mojo Programming Language. Programming in Mojo and Mojo Programming concepts will be discussed further. In the next Article/Blog we will see how Declared variables using var Keyword (the case inside fn Function) helps prevent Runtime Errors caused by Typos with examples.

Thankyou for reading this blog on Variables (Declared and Undeclared variables) in Mojo Programming Language — An Overview.

Reference:

https://docs.modular.com/mojo/manual/

https://docs.modular.com/mojo/manual/basics/

https://docs.modular.com/mojo/manual/variables

--

--

No responses yet