We use the routine
method to automate repeated jobs or specific actions in our program code in Euphoria. The method includes the following routines:
Procedures
Functions
A procedure is a routine that is written to accept parameters. It can be used anywhere inside our code. Read more about procedures in this shot.
In Euphoria, functions are another type of routine that accept parameters and be used anywhere inside our code.
Let’s review some of the similarities and differences between functions and procedures in Euphoria.
They are initialized in the same fashion:
routine_type routine_name(parameters)
In the syntax described above, routine_type
can be a function or a procedure, and the routine_name
can be any Euphoria identifier. In the above syntax, parameters
are the arguments passed to them.
Both can be called at any point in our code, as shown below:
routine_name(paramaters)
The routine_name
maintains the meaning as explained earlier.
Note: Functions can be written as follows:
function my_function(sequence name) return "You are welcome " & name end function
routine_id()
?In Euphoria, routine_id()
is an inbuilt method used to obtain the unique identifier of routine functions and procedures. The id
can be used as a parameter for some inbuilt methods such as call_func()
and call_proc()
. We can even pass this id
as a parameter to a custom-defined identifier. Therefore, we’ve to provide the name of the procedure or function, and the routine_id
of that particular procedure or function will be returned.
routine_id(my_routine)
my_routine
: This is a sequence value, the name of the function or procedure whose routine_id
is to be returned.It will return an integer from 0
upwards and -1
if the routine is not found.
Note:
The routine refers to the id to the
my_routine
parameter and is required to be visible. That is, callable from the position where theroutine_id()
method is used. Otherwise,-1
will be returned.We can indirectly call routine earlier than defined in the program.
--define a couple of sample routinesfunction my_function(sequence name)return "You are welcome " & nameend functionprocedure my_favorite(sequence coding)sequence favorite = "Your favorite of them is " & codingprintf(1,"%s", {favorite})end procedure--call first functionsequence name = "Miya"printf(1,"%s \n",{my_function(name)})--now using the routine_id() get the id these routinesatom id_number = routine_id("my_function")atom id_favorite = routine_id("my_favorite")--not defined yet we shall get the routine_id as wellatom id_hobby = routine_id("my_hobby")-- print it before definitionprintf(1,"%d \n",id_hobby)--now define my_hobby methodfunction my_hobby(sequence coding,sequence sport)return "You have two hubbies " & coding & " " & sportend function--printing the id_numbers to screenprint(1,id_number) -- output is 0puts(1,"\n")print(1,id_favorite) --output is 1
The id_numbers
shown in the output of the code snippet are the numbers that identify the function or procedure in which they were defined before the routine_id()
method was used.
Line 2–7: We define a function and procedure.
Line 14: We call the my_function
and print its output to the console.
Line 18–19: We use the routine_id()
method to get id_numbers
of the routines defined.
Line 22: We use the routine_id()
method to get the id_numbers
of my_hobby()
routine, which is yet to be defined.
Line 25: We print the outcome of line 22.
Line 28: We define the my_hobby()
routine.
Line 33–35: We print values to the console.