Keyboard Input

Variables come in handy when we don't know in advance what values we will be using in our program. What if we called our calculate and print program a "calculator." Technically, that's what it is and does. It calculates two numbers. But what happens when we want to calculate different numbers? We could modify the program and change the numbers, but that's not very efficient if we'll be doing this on a regular basis. It would be best if we get the values from the user at run-time (that is, when the program is run). Let's see how to do that in Euphoria.

Open up your mytest.ex file. Change the program to look like the following:

include get.e
integer x, y
x = prompt_number( "Enter x: ", {} )
y = prompt_number( "Enter y: ", {} )
?x+y

You'll notice two new items in this program: an include statement and a function called prompt_number().

An include statement tells the Euphoria interpreter to "include" the code found in the specified file (get.e). We want to access the code found inside that file, so we can use the include statement to have it made a part of our program. This is extremely useful because we can make use of code that others have already written. There are include files for graphics, windowing, special input from joysticks, and a lot more.

A function is simply a reference to a block of code that exists elsewhere in your program (or in an included file). Calling a function is like running the code it references. Here's a short example program to demonstrate the use of a function:

integer x, y, z

function add( atom x, atom y )
return x + y
end function

x = 1
y = 2
z = add( x, y )

?z

Let's examine this function closely.

function add( atom x, atom y )

First, we tell the Euphoria interpreter that the following lines of code are going to comprise a function called add. Functions must be declared before they can be called! The add() function requires two items of input (called parameters): an atom x and an atom y. Normally, the interpreter immediately executes any commands it encounters. However, in the case of a function, it knows not to run this code immediately. Function code is only meant to be run when "called," so it skips past the code at first.

Our next line

return x + y

tells the interpreter to add the two values it received then return that value to the calling statement. The final line simply tells the interpreter that the block of function code is done.

Now that we've defined the function add(), we can call it from our program.

x = 1
y = 2
z = add( x, y )

We pass the variables x and y to the function add(), which then returns a value which we assign to the variable z.

We could have saved ourselves some time and space by eliminating the variable z and giving the result of add() directly to the ? command, like this:

integer x, y

function add( atom x, atom y )
return x + y
end function

x = 1
y = 2

? add( x, y )

It's just another way to get the job done.

Let's get back to our prior example…

include get.e
integer x, y
x = prompt_number( "Enter x: ", {} )
y = prompt_number( "Enter y: ", {} )
?x+y

Hopefully you know what the include statement does. If not, go back and review! The prompt_number() function is probably self-explanatory, but it is a function provided by RDS in the get.e include file that gets a number from the keyboard.

Remember, a function returns a value to the calling statement. The prompt_number() function gets input from the keyboard and then returns that result. In our program, we call the function and assign the result to x. We then call the function again and assign the returned value to y. When we have our two values, x and y, we print the sum. At least, that's what we hope will happen. Let's run the program to find out.

Cool, huh?! The program prompted you for two numbers, then it output the sum of those two numbers. You can run the program again and use different values. In fact, you could run this program many times and use many different numbers.