Variable Scopes in Mojo Programming Language — An Overview

Shriram Sivanandhan
3 min readApr 18, 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 Variable Scopes 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.

Variable Scopes in Mojo Programming Language:

In Mojo Programming Language, the Variables declared using var Keyword are bounded by Lexical Scoping, where the Nested code blocks can read and modify the Variables defined in an Outer Scope, but the Variables defined in an Inner Scope cannot be read by the Outer Scope.

The above-mentioned concept is explained below in Example Code 1, where we have an if code block which creates an Inner Scope and here the Outer Scope variables are accessible to read/write and the new Inner Scope variables do not live beyond the scope of the if block.

Example Code 1:

Lexical Scopes Example (In New Mojo Playground)

Here in Example Code 1, we had created an Outer Scope variable “a” using var Keyword and also created an Inner Scope variable “a” using var Keyword inside if code block, thereby the Inner Scope Variable “a” hides/shadows the Outer Scope Variable “a” and this is called Variable Shadowing where this prevents the Inner Scope Variable “a” from accessing the Outer Scope Variable “a”. The Lifetime of the Inner Scope variable “a” ends exactly where the if code block ends because that is the Scope in which the Inner Scope variable “a” was defined.

When we define a variable without var Keyword (i.e. undeclared variables) uses use function-level scoping meaning if we change/edit the undeclared variable value inside the if code block, the value of the undeclared variable changes for the entire function.

The above-mentioned concept is explained below in Example Code 2,

Example Code 2:

Function Scopes Example (In New Mojo Playground)

The Last line of output we get the updated value of functional-level scope variable “a”, where this variable value is updated in Inner Scope (if code block).

We had seen an overview on Variable Scopes in Mojo Programming Language. Programming in Mojo and Mojo Programming concepts will be discussed further.

Thankyou for reading this blog on Variable Scopes 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

--

--