Io Programming/Beginner's Guide/Your First Program

Your First Program


When learning to program, the first thing usually learned is how to write a program that prints "Hello, world!" to the console. It gives new programmers a feel of how simple programs are structured. Io has many ways to write programs which do basically the same thing, so at times it can be confusing about which one to use. Writing to the screen is no exception!

Enter the Io's interactive interpreter (by typing io at the console) and type the following:

writeln("Hello, world!\n")

With the interactive interpreter, this should look like:

$ io
Io> writeln("Hello, world!\n")
Hello, world!
==> nil

It looks like writeln is telling Io to write something to the console, specifically the stuff inside the parentheses. But, why parentheses? Nothing else is on the line, so it should be able to figure out what you mean by just printing the rest of the line, right?

Although they are convenient to use for inserting comments into prose, they're also convenient to use to group together all sorts of stuff (like grocery lists, things to do, algebraic expressions, and as this parenthetical expression clearly shows, lists of examples of parenthetical examples!). It is in this second common use that Io uses parentheses: in this case, to group together the stuff you want writeln to deal with. Each item you want to have writeln work with is separated by a comma, like so:

Io> writeln("Hello" , " there!")
Hello there!
==> nil

Notice that, in this case, we gave writeln two things to work with, separated by a comma. If we wanted to, we could have given writeln 50 things to write out, each separated by a comma.

So far, though, we haven't quite answered the question. Let's try a slightly different program, which should help illustrate the point. Just type the following in:

Io> greet := method(
)-> writeln("What is your name?"); you := File standardInput readLine;
)-> writeln("Hello ", you)
)-> )

Io will respond with:

==> method(
    writeln("What is your name?"); you := File standardInput readLine
    writeln("Hello ", you)

Don't worry about what all that gobbledyguk means. What we basically did was write a very simple Io program -- indeed it's your very first! Let's run it:


It'll respond by asking you for your name. Go ahead and type it in:

What is your name?
Hello Sam
==> nil

The answer to the question of why write needs those parentheses is right there in the above program. If writeln just consumed the remainder of the line, then it would not be possible to string together multiple things for the computer to do. It probably would have printed out something like this instead:

Io> greet
What is your name?
you := File standardInput readLine
 Exception: Object does not respond to 'you'
 Object you                           greet Line 3

Whoa! What is all this mess? We'll see what these things are later in this book. Fortunately, we have those parentheses to keep us on the right path.

Now, all this time I've been calling everything inside those parentheses things. Well, they are, but they have a more correct name for them. Instead of constantly calling these things things, we choose to call them arguments instead.

With all these parentheses and arguments flying around, this might start to sound like what you learned way back in Algebra class at Paleozoic High. You're right -- this notation comes from the concept of algebraic functions. writeln is said to be the function name, and both "Hello " and you are its arguments.··


writeln is not a true function in the algebraic sense. It is more correctly described as a procedure, and you might hear this term used now and again. But, there is still a third term for it, called a method, for reasons that will be made clear later in this book. For now, we can ignore these differences.