Functions

A function is a group of code bundled together under one command. For instance, consider the following:

function fix( atom a, atom b )
atom c, d
   c = a + b
   d = a - b
   return c * d
end function

atom z

?fix(3,4)
?fix(45,13)
?fix(6,143.3)
z = fix(72,44)

We created a function called fix() that consists of two lines of commands. If we weren't using the function, our code would look like this:

atom a, b, c, d, z

a=3
b=4
c = a + b
d = a - b
print(1,c * d)

a=45
b=13
c = a + b
d = a - b
print(1,c * d)

a=6
b=143.3
c = a + b
d = a - b
print(1,c * d)

a=72
b=44
c = a + b
d = a - b
z = c * d

You can see how more efficient our first program is! Now, imagine if we wanted to change the way we "fixed" the numbers. In our second example, we would have to change three instances of the code. In our first example, we have to change only one instance- the code inside the function.

Functions provide a return value. That is, when you call a function, it gives you a value. In early versions of Euphoria, you had to assign that value to a variable. Starting with version 4.0, you do not have to assign the return value.

As an example, consider the following code:

k = wait_key()
wait_key()

In versions of Euphoria prior to 4.0, you had to assign the result of wait_key() to a variable (like on the first line above), so the second line in the above code would produce an error message. In 4.0, the above code runs just fine, making you press a key on your keyboard twice. The first time, it assigns the result to the variable k. The second time, it ignores the result of the function and continues the program.