Basics of Programming, Syntax and Semantics, Functions, Variables in Mojo Programming Language: Programming in Mojo — Part III

Shriram Sivanandhan
4 min readSep 22, 2023

--

Mojo Programming Language has the usability of Python but with the performance of C, C++ and Rust. Mojo Programming Language utilizes features like caching, multithreading and cloud distribution technologies. Also, Auto-Tuning and compile-time Metaprogramming features allows to write code for several hardware. Modular-Mojo Development Team claimed that Mojo Programming Language is 68000x faster than Python by using/evaluating Mandelbrot Algorithm on h3-standard-88 Intel Xeon Instance.

Programming in Mojo:

In this Blog, Programming in Mojo is done using Mojo Playground. Mojo Programming Language is a Compiled Language and its performance and memory-safety features are due to the fact Mojo is a Compiled Language. Mojo Programming Code can be Ahead-Of-Time (AOT) or Just-In-Time (JIT) compiled.

Syntax and Semantics:

Mojo Programming Language supports (or will support) all the Python Programming Syntax and Semantics. For Instance, like Python Programming Language, Mojo Programming Language uses code indentation and line breaks to define code blocks and not curly braces. Also, Mojo Programming Language supports all the Python Programming Language control-flow syntax like for loops, if condition, while loops, etc. Since, Mojo Programming Language is still in development some features of Python Programming Language are not yet implemented in Mojo, but all the missing Python features will be implemented soon as mentioned by Modular-Mojo Development Team. But Mojo Programming Language already consists of many features and capabilities beyond Python Programming Language.

Functions:

Similar to Python, function is defined in Mojo Programming Language using def keyword and also using fn keyword. In this Article/Blog, we are going to see function declared using fn keyword only. The functions declared using def keyword provides Python-style Dynamic behaviors, functions declared using fn keyword enforces strongly-typed and memory-safe behaviors. Both fn and def keywords are used to declare functions in Mojo Programming Language but using functions declared using fn keyword behaves differently and it will be discussed later.

Print HELLO MOJO!!! with Function created using fn keyword

Variables:

In Mojo Programming Language, variables can be declared using var keyword and also using let keyword. var keyword is used to create variable which is mutable (i.e the value of the variable declared using var keyword is mutable). let keyword is used to create variable which is immutable (i.e the value of the variable declared using let keyword is immutable).

Example:

Variable declaration using var keyword can be seen in the below example, and also, we can see that the value of variable “a” is mutable.

Variable declaration using var keyword

Variable declaration using let keyword can be seen in the below example, and also, we can see that the value of variable “a” is immutable.

Variable declaration using let keyword
Value of variable “a” is immutable

In the above example, error raises because when we declare a variable “a” using let keyword with value 10 and then change the value to 20 — there we will get a compiler error, value of the variable declared using let keyword is immutable.

Functions declared using fn keyword requires explicit variable declarations (i.e let or var variable declarations) but Functions declared using def keyword doesn’t require explicit variable declarations and it is optional. Also, in the above example variable “a” has an explicit Int type specification, Type Declaration for variables is not required for functions declared using fn keyword, but it is desirable sometimes and if it is omitted, Mojo infers the type.

Type Declaration for variables is not required for functions declared using fn keyword, but it is desirable sometimes and if it is omitted, Mojo infers the type

We had seen about Basics of Programming in Mojo, Syntax and Semantics, Functions, Variables in Mojo Programming Language. Programming in Mojo and Mojo Programming concepts will be discussed further.

Thankyou for reading this blog on Basics of Programming, Syntax and Semantics, Functions, Variables in Mojo Programming Language: Programming in Mojo — Part III.

Reference:

https://docs.modular.com/mojo/programming-manual.html

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

--

--