Euphoria - Functions
Euphoria functions are just like procedures, but they return a value, and can be used in an expression.
This section will explain you how to write your own functions in Euphoria.
Function Definition:
Before we use a function we need to define that function. The most common way to define a function in Euphoria is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block which ends with end function statement. The basic syntax is shown here:
function functionname(parameter-list)
statements
..........
return [Euphoria Object]
end function
|
Example:
A simple function that takes no parameters called sayHello is defined here:
function sayHello()
puts(1, "Hello there")
return 1
end function
|
Calling a Function:
To invoke a function somewhere later in the script, you would simple need to write the name of that function as follows:
#!/home/euphoria-4.0b2/bin/eui
function sayHello()
puts(1, "Hello there")
return 1
end function
-- Call above defined function.
sayHello()
|
This would produce following result:
Function Parameters:
Till now we have seen function without a parameters. But there is a facility to pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters.
A function can take multiple parameters separated by comma.
Example:
Let us do a bit modification in our sayHello function. This time it will take two parameters:
#!/home/euphoria-4.0b2/bin/eui
function sayHello(sequence name,atom age)
printf(1, "%s is %d years old.", {name, age})
return 1
end function
-- Call above defined function.
sayHello("zara", 8)
|
This would produce following result:
The return Statement:
A Euphoria function must have return statement before closing statement end function. Any Euphoria object can be returned. You can, in effect, have multiple return values, by returning a sequence of objects. e.g.
If you have nothing to return then simply return 1 or 0. Here 1 can indicate success condition and 0 can indicate failure condition.
|