Printing to the Outside World

Now that we have the environment setup we’re going to write the traditional Hello World program. In this tutorial, all text associated with a Tcl command is bolded like this. All of the examples can be easily copied to your clipboard by hovering over the example code box and clicking the word “copy” that appears in the upper right-hand corner of the box. You can then paste the code in the coding widget at the bottom of this tutorial and run the code.

The command to output text to the window or console is

puts argument1

where putsstands for put string.  After puts executed a new line is automatically inserted. If argument1 is multiple words long (i.e. there are spaces) then it must be enclosed in quotations or braces. Be sure to always leave a space between the command and the arguments even when the arguments are wrapped in quotes or braces.

puts Hello
puts World!
puts "Hello World!"         
puts {Hello World! with braces}

Notice that commands are delineated by new lines and therefore do not require a termination character.

Comments in Tcl are started using the # character. A comment can either exist on its own line or it can follow a command. However, if the comment follows a command then the command must be terminated with a ; semicolon.

#I’m a comment living on my own line
puts "Something meaningful"; #Notice the semicolon before this comment

To create your own program make a file called hello.tcl using your favorite text editor and type any of the previous commands (just to make sure that you save it as a .tcl file). Then execute the script by typing tclsh hello.tcl.

Exercise
Think you understand how puts works? Correct the following piece of code and get it to run. Try printing your name or favorite phrase.

JOIN OUR NEWSLETTER
I agree to receive email updates when new content is posted and receive promotional emails when new courses are available
Enjoying the article? Receive free updates when new content is published and courses are available.
We hate spam. Your email address will not be sold or shared with anyone else.
Christopher Hogstrom
christopher.hogstrom@grittyengineer.com

Leave a Reply

Your email address will not be published. Required fields are marked *