A Closer Look at “Hello, World!” in CX

What the simplest program can teach you about Skycoin’s revolutionary blockchain programming language.

Fray
Skyfleet Captain’s Log
4 min readApr 21, 2019

--

Say hello to the world of CX.

The tradition of beginner coders learning to code with a “Hello World” exercise has persisted because it is typically a very simple program to write while introducing learners to the basic structure and syntax of the language.

Just like other programming languages, the best way to learn CX is to start with “Hello, world!”, so let’s examine this specific program to see what it can teach us about writing CX code. This is what it looks like:

package main
func main () {
str.print("Hello, world!")
}

Let’s start with the first line:

package main

Every program in CX is organized in packages. CX uses modularization, meaning a program can be broken into distinct packages to avoid making any given program file too large or unwieldy, as long as all those packages are in the same directory or workspace. In the same way that the C# language has namespaces or Python has modules, CX has packages.

Your main package has been delivered.

Every CX program must have at least one “main” package. “Hello, World” is a simple program, so it only has one package which therefore must be main.

func main () {

Just as every CX program must have a main package, every CX program must declare a main function. Once that function is declared, the program knows that anything that falls between the curly brackets is part of that function.

Your main function has been delivered, too.

Defining this function is your CX program’s entry point, and the main function will be run first, telling the computer running the program what to do next. A function is a subroutine of the program, that can be executed by calling it’s name; in this case, “main”. This allows a programmer to build frequently used operations into functions that can be called whenever they’re needed, rather than typing the function out in full each time.

The parentheses are required, but in this case, they do not contain any data. In more complex functions, you would place input or output parameters in-between these parentheses to modify the way the function operates. These parameters allow a given function to behave differently, depending on what data is sent to it.

str.print(“Hello, world!”)

Here is the meat of the function, nestled between two curly brackets. This is the part where the program tells the computer to print Hello World, which really is the main function of the program.

str. is short for string. CX is characterized as a strict typing system; this feature means there tends to be a type-specific function for achieving a given task. “Integers” store whole numbers, “strings” store text data, and “floats”, or floating-point datatypes, store decimal data, like a number with a fractional component in decimal notation.

“Strings” hold feline attention and text data.

The program is invoking the string datatype with the function print. You can also think of str.print as the print function in the str package. Every datatype has a package that is used to group the functions for that datatype. (So technically it isn’t the datatype per se that has the function, but rather the package associated with it.)

The parentheses contain the value of the datatype you wish to print, which in this case is “Hello World”. The quotation marks isolate the phrase and prevent the program from trying to interpret the phrase as code. (Note: be sure they are straight and not curly quotes).

This line is interpreted by the computer to mean “print the string “Hello, world!” to the standard output stream.”

The closing curly bracket } closes the function, and also our Hello World program.

After you’ve written your program in your favourite text editor, you can save it with the extension .cx and have an executable CX program application.

When your run the program on your computer, your screen should display the words “Hello, world!” And that’s it!

Hey, it worked!

One could also run the program as interpreted code, rather than compiled code, but it can run it either way, or both ways, since CX can work with both compiled and interpreted code at the same time. As you learn more about CX, or write more complicated programs, you will see why one approach or a combination, may be preferable from a performance standpoint.

Check the links below to learn more about CX, and to continue learning. Good luck on your CX programming journey!

CX Textbook: https://cx.skycoin.net/books/cx-programming-language &
https://github.com/skycoin/cx/releases

Skycoin’s CX page: https://www.skycoin.net/cx/

CX on GitHub: https://github.com/skycoin/cx

CX Telegram Group: https://t.me/skycoin_cx

If you liked my writing and would like to contribute to me making more feel free to donate some Skycoin: GCB5KK9LmJzxxxh2hMoKm3HRXwaJe9vRfd

--

--