How the Hello Sketch Code Works

A function is a container for statements (lines of code) that tell the Arduino to do certain jobs.  The Arduino language has two built-in functions: setup and loop.  The setup function is shown below.  The Arduino executes the statements you put between the setup function’s curly braces, but only once at the beginning of the sketch.

In this example, both statements are function calls to functions in the Arduino’s built-in Serial pre-written code library: Serial.begin(speed) and Serial.print(val). Here, speed and val are parameters, each describing a value that its function needs passed to it to do its job.  The sketch provides these values inside parentheses in each function call.

Serial.begin(9600); passes the value 9600 to the speed parameter. This tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second.  That’s 9600 binary ones or zeros per second, and is commonly called a baud rate.  

Serial.print(val); passes the message “Hello!” to the val parameter.  This tells the Arduino to send a series of binary ones and zeros to the Serial Monitor. The monitor decodes and  displays that serial bitstream as the  “Hello!” message.

The Arduino setup function calling Serial.begin and Serial.print

After the setup function is done, the Arduino automatically skips to the loop function and starts doing what the statements in its curly braces tell it to do.  Any statements in loop will be repeated over and over again, indefinitely.  Since all this sketch is supposed to do is print one "Hello!" message, the loop function doesn’t have any actual commands. There’s just a notation for other programmers to read, called a comment.  Anything to the right of // on a given line is for programmers to read, not for the Arduino software’s compiler.  (A compiler takes your sketch code and converts it into numbers—a microcontroller’s native language.)

An empty loop function

What is void?  Why do these functions end in ()? The first line of a function is its definition, and it has three parts: return type, name, and parameter list. For example, in the function void setup() the return type is void, the name is setup, and the parameter list is empty – there’s nothing inside the parentheses (). Void means ‘nothing’—when another function calls setup or loop, these functions would not return a value.  An empty parameter list means that these functions do not need to receive any values when they are called to do their jobs.