Saturday, May 4, 2013

Summer of Code: Day 1

As some of you might have read in my introductory post, I am a big fan of Archlinux and have been using it for a while. Thus, I am no newcomer to linux commands and use my terminal to avoid as many GUI's as I can manage. That being said, I admittedly have much less programming experience than I probably should. For that reason, I would like to spend every day working on some sort of code. For today's project, I would like to print the famous "hello world" in five languages: Python, Lua, C++, Java, and Fortran.


Python

It seems to me that every other time I see a new package in the AUR, it is coded in Python. For some reason, Python has become the language of choice for many linux users. It may not be as fast as C++ or Java, but it is most definitely easy to learn and fun to write. In my case, I learned the language for somewhat mischievous reasons. See, I would often leave my laptop open in the Society of Physics Student's room at my college, and a friend of mine would get on and type random obscenities into the terminal. Because of this, I wrote a series of python "commands" for every curse word I could think of. Now, every time he curses into my terminal, something happens.

Regardless, here is Hello World in Python:

File helloworld:

          print("hello world!")

Execution:

          python helloworld

Though officially, I should have added a ".py" extension to the end of my file, it worked just as well without it.


Lua

I know what you are thinking, "Lua? Of all languages to learn, why lua?" Well, lua is a vastly underappreciated language with possibly fewer possibilities than python, but with a much lighter footprint. In my case, I wanted to learn a bit about it because of my internet browser, luakit. If you have not had the chance to try out this browser, you might want to give it a go. It is lightweight and customizable. Anyway, here is Hello World in lua:

File helloworld:

          print("hello world!")

Execution:

          lua helloworld

Two quick notes: 
1. I did not change the file "helloworld" from python (which may or may not have been why I kept it extensionless in the first place). If this were more proper, I would have added a ".lua" extension on the end of the file.
2. There is an alternate version of "Hello World" in lua that I do not understand yet, but find absolutely remarkable on Corsix.org. I would suggest checking it out if you have the time. Just copy and paste the text into a file, save the file and compile it like I did before. 


C++

Now for the big guns of C++ and Java. Chances are, if you have ever had dreams of programming just about anything, you might have been informed about C++. It is a fantastic language, used in game programming, Operating Systems, and computational physics. Truth be told, there is a little more baggage to the language, but it is definitely worth it to learn if you intend to write software for a living. It is fast, fierce, and downright fancy. Well, maybe not the last one. Here's "Hello World" anyway:

File helloworld.cpp

          #include <iostream>

          int main()
          {
                std::cout << "hello world\n";
          }

Now, what is this code doing? Let's disect it line by line:

          #include<iostream>

This line allows the program you write to output data to the screen. In C++, there is an effective "header zone" at the top of every file. This section essentially creates functions for the rest of the file. In this case, we called a header from the standard library. We could have called functions from another file we created, which is really cool, when you think about it.

         int main()

This initializes the main funcion, an essential part of every c++ code. Your codes will not compile correctly without this function.

          {
                std::cout << "hello world\n";
          }

The brackets open the main function to its contents. "std::cout" creates a calles upon the iosteam header (above), and outputs "hello world" into the terminal. "/n" closes the line. In most files, there will also be a << endl; before the end of the file. Also note that the semicolon is essential to compilation. 

Compilation:

          g++ helloworld.cpp -o helloworld

Here we are using g++ as a compiler. The "-o" command outputs the compiled file's contents to an executable file, in this case named "helloworld." 

Execution:

./helloworld

Unlike the Python and Lua scripts, the compilation and execution steps are separated. This is common for most programming languages. You'll get used to it the more you do it.

And that's "Hello World" in c++!


Java

Java is a bit of an oddball for me. I figured I should learn it simply because of it's importance to Android development. It is easily the most learned language in college and has many uses. It is definitely a good language to know. 

File helloworld.java

          public class helloworld {

               public static void main(String[] args) {
                   System.out.println("Hello World");
               }
          }

Like in C++, well split this up line by line.

          public class helloworld {

This line creates a class, an important concept in all languages, but especially so in java. The class, in this case, is "helloworld." Note that the word "public" is simply defining the type of class.

               public static void main(String[] args) {

This line calls the class we just made and adds a string with arguments in the next line. "Strings" in programming are simply words (not numbers).In almost all cases when programming, if you see an open bracket, the next line is dependent on the line above it.  

                   System.out.println("hello world");
               }
          }

These lines, output the string "hello world" to your terminal and then close the definition of the class and the class, itself. 

Compilation:

          javac helloworld.java

Execution:

          java helloworld

Java is a bit of a quircky language. Unlike the other languages, you must name the file as the class for the code to compile. Also, the language does not directly output an executable file. Rather, you must compile it with the "java" command. There are many advantages to using Java instead of C++, namely because java was designed with a "write once, run anywhere" approach, meaning the user does not have to recompile something on their own system. This is great for exporting widgets and games, but because java requires a runtime environment (unlike C++), users are required to have some form of java on their system. This is not so big of a deal nowadays because of java's popularity, but it is something to keep in mind. 


Fortran

I have often heard Fortran referred to the ents of the programming world. It is oldand slow, but dependable. That being said, Fortran is only two out of three of those. Though Fortran is old, it is still decently fast--much faster than Python or Lua. In fact, it is still widely used in computational physics. It might not be a language for everyone, but if you need to learn it, possibly because it has become popular in your area of expertise, here it is. "Hello World" in fortran:

helloworld.f

      program hello
         print *, "hello world"
      end program hello

Now, fortran is a bit of an odd language. It is easier to learn than either Java or C++, but a little more difficult than python or lua. In this case, be sure to type in six spaces exactly before every line. Fortran is program oriented, using the "program" function kind of like java uses the "class" function. That being said, fortran is oriented towards physicists and the mathematically minded. For now, it is sufficient to think of the "*," as a short version of "(*,*)," or an "(x,y)" set of values (like in math). In this case, it has only an x-value of "hello world." This isn't really accurate, but it is a good way to think about how this program, in particular works.

Compilation:

          gfortran helloworld.f -o helloworld

Execution:

          ./helloworld

These are like C++, as are many other programming languages. Again, Fortran isn't for everyone. It is mainly for those who are entering fields that already have a strong code-base in the language.


And that's that! Hello World in 5 languages, all explained, and all on day 1. There are plenty of days left, feel free to comment if you have any ideas of what I should do. 

Thanks for reading,
-Leios

2 comments:

  1. The crazy "hello world" from lua kinda reminds me of font source code.

    ReplyDelete
    Replies
    1. Honestly, I was astonished to find out that it actually compiled correctly.

      Delete