An Overview of How Declared variables using “var” Keyword inside the function declared by using “fn” Keyword helps prevent Runtime Errors caused by Typos in Mojo Programming Language
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 of How Declared variables using var Keyword inside the function declared by using fn Keyword helps prevent Runtime Errors caused by Typos 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.
An Overview of How Declared variables using “var” Keyword inside the function declared by using “fn” Keyword helps prevent Runtime Errors caused by Typos in Mojo Programming Language:
When we use Undeclared variables inside the function declared by using def Keyword and if we misspell the name of the variable, then Mojo Programming Languages instantiates a new variable using the misspelled variable name.
Example Code 1:
In the above Example Code 1, an Undeclared variable named variable_2 is created and Initialized with a value and after we had misspelled this variable_2 as varible_2 and assigned a value to it and here Mojo Programming Languages instantiates a new variable using the misspelled variable name.
When we use Declared variables using var Keyword inside the function declared by using fn Keyword helps prevent Runtime Errors caused by Typos, because inside the function declared by using fn Keyword we should first declare all the variables using var Keyword and if we misspell the name of the variable, then these errors are caught by the Compiler.
Example Code 2:
In the above Example Code 2, we had used Declared variables using var Keyword where we had Declared and Initialized a variable with a value with the variable named variable_2 and after we had misspelled this variable_2 as varible_2 and assigned a value to it and this is an Error which is caught by the Compiler.
By this way, Declared variables using var Keyword inside the function declared by using fn Keyword helps prevent Runtime Errors caused by Typos in Mojo Programming Language.
But we can also use Declared variables using var Keyword in a function created by def Keyword, but this benefit is realized only when Declared variables using var Keyword is used inside a function created by fn Keyword, where the Mojo Programming Language Compiler will flag the usage of Undeclared variable (E.g. varible_2 in above Example Code 2) as unknown declarations.
We had seen an overview of How Declared variables using var Keyword inside the function declared by using fn Keyword helps prevent Runtime Errors caused by Typos in Mojo Programming Language. Programming in Mojo and Mojo Programming concepts will be discussed further.
Thankyou for reading this blog on An Overview of How Declared variables using var Keyword inside the function declared by using fn Keyword helps prevent Runtime Errors caused by Typos in Mojo Programming Language.
Reference:
https://docs.modular.com/mojo/manual/