The All-Purpose Programmer: Episode 1 - "Hello World" by@halexmorph
563 reads

The All-Purpose Programmer: Episode 1 - "Hello World"

tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

Jacob Landry is on a journey to enhance his resume with a wealth of new programming languages. For the past 12 years, he has specialized in PHP, though he’ve always dabbled with other languages. This series of articles is meant to serve two purposes: an example of how I learn new languages. It’s going to serve as a bit of a tutorial or entry-level-learning opportunity for anyone interested in the languages I focus on. I start with learning how to print to screen and store basic data. I then move to more complicated data structures and control structures. Last, I learn their OOP (if applicable) syntax.

People Mentioned

Mention Thumbnail

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - The All-Purpose Programmer: Episode 1 - "Hello World"
Jacob Landry HackerNoon profile picture

@halexmorph

Jacob Landry

Senior Software Engineer On an exciting journey to learn new...

About @halexmorph
LEARN MORE ABOUT @HALEXMORPH'S EXPERTISE AND PLACE ON THE INTERNET.
react to story with heart

Code Examples and Run Instructions are now located on GitHub

I’m on a journey to enhance my resume with a wealth of new programming languages. For the past 12 years, I have specialized in PHP, though I’ve always dabbled with other languages.

There is the obvious choice for specialists in the web industry: Javascript, but I have also dabbled with Python, Perl, Java, Swift, and Bash scripting. Recently, though I dearly love PHP, I’m just not seeing the career trajectory in the PHP space that I once saw and am taking other languages more seriously.

Instead of dabbling with them, I’m moving into full-on hobby projects or courses to learn more about the languages at a deeper level.

This series of articles is meant to serve two purposes. First, it’s going to be an example of how I learn new languages. This might not work for everyone, but it has worked well for me when I’ve needed to pick up a language in a new project over the past 12 years. Second, It’s going to serve as a bit of a tutorial or entry-level-learning opportunity for anyone interested in the languages I focus on.

Learning

The method for me is simple. I start with learning how to print to screen and store basic data (Hello World + Variables). I then move to more complicated data structures and control structures. Last, I learn their OOP (if applicable) syntax. At this point, though I’m far from an expert, I feel quite comfortable reading other peoples’ code or writing my own. This simple process is enough to at least understand the answers provided on Stack Overflow enough to implement them, which is all many of us do anyway.

Tutorial

I’m going to treat this series as a shortlist of lessons. My plan, though it may change as I get writing, is to create the following articles:

  1. Hello World - simple variable declarations, functions, and displaying data to the screen
  2. Everything Varies - Diving further into variable declarations with commonly used data types; how to declare them, how to use them, how to interact with them.
  3. Classes and Objects - The basics of OOP in each language (where applicable)
  4. Simple Project - A simple, fully functional project that uses OOP code, includes some basic libraries provided by/for the language and includes some form of user input/output.

Languages

Again, this is subject to change based on feedback but, based on my own desires, I plan to feature the following languages in every article.

  • PHP (my current expertise, just to set a basis)
  • Javascript
  • TypeScript
  • Python
  • Java
  • C/C++
  • Go
  • Scala
  • Rust

These languages are chosen based on my own interests and also based on the popularity that I’ve seen through conversations with others in the industry or job postings that I’ve reviewed. I’m open to suggestions if there’s a critical language that many are interested in that I’ve missed. I’m not sure i’ll remove anything from the list, but i’m sure it’ll grow.

Let’s Get Started!

Hello World! The simplest, most commonly accepted way to start learning any new programming language but I’ve always found one major flaw with it. It’s useless. Yes, you learn to output data to the screen but not in a presentable manner. If anything this teaches you how you’ll debug your code as a beginner, before getting into the more common debug frameworks and methodologies. It’s just not a good starting point.

A starting point should be basic, yes, but not so basic that it’s boring. I want to still be awake when I’ve completed my first task. So I usually add a very simple change that makes the exercise feel more valuable: string variable declarations.

PHP

So I had written out this long step by step instruction manual on how to construct this basic code block, different choices made along the way. I started simple and slowly complicated it…and I got bored editing it, so I can’t imagine how boring it would be to read. Lets do this. If you’re reading this, you’re interested in learning something. So lets break it down in bite-size chunks that won’t put you to sleep.

Consider the following code:

<?php

$firstName = "Jacob";
$lastName = "Landry";

echo hello($firstName, $lastName);

function hello(string $first, string $last): string
{
  return "Hello " . $firstName . " " . $lastName . ", how are you doing today?";
}

?>

Output:

Hello Jacob Landry, how are you doing today?

We did three major things here:

Variables

Declaring variables in PHP is done with the $ symbol followed by a unique identifier. You can assign data to the variable with the equals sign. Variables are dynamically typed in PHP, which means you could overwrite any of those strings with an integer and no one would care (except for me, I care, don’t do that).

Functions

Declaring functions is done by using the word “function” followed by an identifier. Variables can be passed into a function the same way they are created. We do have the added opportunity (as of PHP 7) to declare a type-hint here.

These type-hints are not required, and the code will work fine without them, but it gives us an opportunity to catch any issues or errors, and prevent ourselves or someone else from using this function incorrectly in the future. This is done by passing the type before the variable, in the function. string $firstName.

You can return data from the function with the “return” directive. You can also provide a non-required type-hint for the return value as well (as of PHP 8) with :type at the end of the function, before starting the internal code. I do this above with ): string but this is not required. Without this, you can return any data from the function without complaint. With it, you can still return anything you want but anything outside of a string will generate a warning.

Output

Outputting data in PHP is done using “echo” or “print”, in most cases. Here I’ve chosen echo. This will print the string to the browser or terminal, depending on how the code is being executed.

Javascript

Javascript has a lot of similarities with PHP (lets be frank, most languages are remarkably similar at this basic level), so the same code looks as follows:

var first_name = "Jacob"
var last_name = "Landry"

console.log(hello(first_name, last_name))

function hello(first_name, last_name) {
  return "Hello " + first_name + " " + last_name + ", how are you doing today?"
}

Variables

Variables can be declared with var, let, or const in Javascript. I’ve chosen var here for simplicity, maybe we can get into the other declarations in the future. You set data to the variable with the equals sign followed by the data. (Note: you can actually drop the “var” directive and it works just fine but…it feels dirty). Javascript is also dynamically typed so there is no type provided for the variables, and as far as I (or google) seem to know, there isn’t a way to provide hints.

Functions

Functions are declared with the word “function” followed by an identifier. You pass in variables by listing them inside the function input block. You return data with the “return” directive.

Output

Outputting data can be done in several ways in Javascript. Normally you would likely be editing the DOM on your website, but here we are simply outputting to the Javascript console in your browser. This is a simple way to debug early applications, though I wouldn’t bank on it as your skills and applications grow.

TypeScript

var first_name:string = "Jacob"
var last_name:string = "Landry"

console.log(hello(first_name, last_name))

function hello(first_name:string, last_name:string):string {
    return "Hello " + first_name + " " + last_name + ", how are you doing today?"
}

Variables

Declared by “var” followed by an identifier. A type can be provided with “:type” but is not required. The code will execute just fine without the type declared, or even if you break your type rules by assigning a number to a string. You will see some warnings, which are important to take note of, but the code will function fine.

Functions

Functions are declared with the word “function” and an identifier. You can add types to your variable inputs and as an output. Same as above, this won’t actually break anything but is nice to have for sanity reasons.

Output

Similar to Javascript, output can be achieved in a few ways but for this simple example console.log will output the data to the Javascript console in your browser.

Note: Typescript is Javascript with Type-hinting. So the code is exactly the same but with stricter rules around data-types.

Python

first_name = "Jacob"
last_name = "Landry"

def hello(first_name, last_name):
  return "Hello " + first_name + " " + last_name + ", how are you doing today?"
}

print(hello(first_name, last_name))

Variables

Similar to Javascript and Typescript, a variable can be declared simply by typing the identifier, followed by the = sign and the value. There is no static typing or declaration needed.

Functions

Functions are defined by the word “def” followed by the identifier and a colon. Everything inside the function must appear after the colon and indented. I did find that I could not call “hello” before it was defined, which is why I had to change the order of the code here.

Clearly this would not be an issue in an OOP approach, but for something like this it was worth noting. Data is returned from the function with the “return” directive.

Output

Data can be output with print() and strings are concatenated the same as with Javascript with the “+” character.

Java

Note, this is where things start to get a little more defined. Java is a statically typed language and everything must be executed from within a class. This filename is helloworld.java and when you run it, it automatically executes the main method.

public class HelloWorld {

	public static void main(String[] args) {
		String first_name = "Jacob";
		String last_name = "Landry";

		System.out.println(hello(first_name, last_name));
	}

	public static String hello(String first_name, String last_name) {
		return "Hello " + first_name + " " + last_name + ", how are you doing today?";
	}

}

Variables

Java is a static typed language, which means the variable types must be defined. Once they are defined, they cannot change. So in this example I have defined the class’ first_name variable as a String. Now, any time I assign a value to this variable it must be a string or the program will not compile and run. Variables are defined by the type followed by the identifier, then the = and value.

Functions

Functions are defined by their level of access (public, private, protected), then their return type (String), followed by the identifier. In this case I have also opted to make the function “static” which means it can be used without a created object. This just made the example easier to work with so we didn’t have to create a new class/object to run the function.

Also, you’ll notice that you have to define the variable types for input as well. If you try to pass anything other than a string to this function the program will not compile and run. Data is returned from the function with the “return” directive.

Output

Output can be done in many ways but for this example we simply output to the console with System.out.println. Strings are concatenated with “+”, similar to other languages.

C/C++

#include <iostream>
#include <string>

using namespace std;

string hello(string first_name, string last_name)
{
    return "Hello " + first_name + " " + last_name + ", how are you doing today?";
}

int main()
{
    string first_name = "Jacob";
    string last_name = "Landry";


    std::cout << hello(first_name, last_name);
}

Variables

C/C++ is statically typed, so you declare variables by choosing a type, followed by the identifier, then the value.

Functions

Functions are defined similarly to variables by starting with their return type, followed by the identifier. Variables must have types as well when passed into the function.

Data is returned from the function with the return directive.

Output

Output, this example, is sent to the terminal with std::cout . This uses the “standard” library (std) and the “character out” function (cout). Strings can be concatenated with the “+” symbol, though I did also see some examples where they could be concatenated with “<<“ though I’m sure this does something slightly more complex so I would want a better understanding of it before I use it.

Go

package main

import "fmt"

func main() {
    var first_name string = "Jacob"
    var last_name string = "Landry"

    fmt.Println(hello(first_name, last_name))
}

func hello(first_name string, last_name string) string {
    return "Hello " + first_name + " " + last_name + ", how are you doing today?"
}

Variables

Variables are declared with the “var” directive followed by the identifier and value. Optionally, you can provide a type as well, after the identifier, but it is not required. It seemed that this could help create default values for variables so they were not null before being initialized: var number int would default to 0, for example. I’m not sure if this is really the use-case for this feature or not, though. Either way, assigning types when possible is a good idea IMO.

Functions

Functions are declared with the func directive, followed by the identifier. While variable types are optional, they seem to be required inside the function and appear after the variable name. You must also provide the function’s return type after the variables if you are returning data. Data is returned with the “return” directive.

Output

In this situation we output to the screen with the “Format” (fmt) package. This outputs data to the terminal as in all the other examples. Strings can be concatenated with the “+” symbol.

Scala

object Hello {

    def main(args: Array[String]) = {
        var first_name : String = "Jacob"
        var last_name : String = "Landry"

        println(hello(first_name, last_name))
    }

    def hello(first_name : String, last_name : String) : String = {
        return "Hello " + first_name + " " + last_name + ", how are you doing today?"
    }

}

Variables

Variables are defined by the var directive, followed by the identifier and the value. Optionally you can provide a type here with the format : Type after the identifier but before the value.

Functions

Functions are defined by the “def” directive followed by an identifier. Variables being accepted into the function require types, unlike variable declarations which are optional. It is also required to provide the return type for the function after the list of variable inputs. Data is returned with the “return” directive.

Output

Output to the terminal is created with println, which is clearly very common. Strings can be concatenated with “+” (again, common apparently).

Rust

fn main() {
    let first_name = "Jacob".to_string();
    let last_name = "Landry".to_string();

    println!("{}", hello(first_name, last_name));
}

fn hello(first_name: String, last_name: String) -> String {
    return format!("Hello {} {}, how are you doing today?", first_name, last_name);
}

This was, by far, the most confusing for me so I’m looking forward to digging into the language more.

Variables

Variables are defined by the “let” directive, followed by the identifier and a value. It seems that, by default, all variables are immutable, so if you’ll need to modify this variable later you have to add the “mut” directive after “let”. Types are not defined, and as far as I could see in my first searches, there isn’t a way to do so.

Functions

Functions are defined by the “fn” directive, followed by the identifier, list of params (with types) and a return type signified by “→ Type”. Data is returned with the (you guessed it) “return” directive.

Output

Output to the terminal in this case was generated with a println() call. This call takes a format string where variables replace “{}”. You’ll see I had to use a similar formatter in the return of the function as well. This was how I concatenated the strings. I could not find another way similar to the other examples where you “add” strings together, instead I found that I needed to pass them into this formatter to build the string I wanted. It was confusing at first but I actually like it better. I’m sure there are similar constructs in the other languages that I could go back and employ.

There you go!

You now have working “hello world” examples in each language, along with examples on variable and function declarations and some basic string concatenation. This basic crash course is a great way to hit the ground running in any language, I’ve found because it lays the building blocks for everything you’ll do in the language in the future.

Just to note, something I did not touch on here is how to install and execute these files in each language. Some languages require you to build the code first then execute a binary. Others you can simply execute the code as it’s written.

This varies from language to language. I think, in the future, I might write another short series on how to set up/debug each language (as I learn more about that myself) but for now, if you want to run any of these examples yourself either find me on twitter and ask some questions or find the (usually amazing) documentation for the language(s) you want to try.

They should have everything you need there to get the language installed on your development machine and start hacking away.

Be sure to keep an eye on my profile for the next post. I’ll dive a little further into all of these languages with a look at common data types and how they’re used or implemented in each language.

I had planned to provide an article a week but this took far longer to write and edit than expected, but I’ll try to produce them as regularly as possible. I’m aiming for every-other-week, so stay tuned and stay safe!

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa