What is the routine_id() method in Euphoria?

Overview

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

Procedures

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.

Functions

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.

Similarities between functions and procedures

  • 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.

Differences between functions and procedures

  • A procedure in Euphoria cannot return any value as a return value. Hence, if we want to get a value from a routine, the function structure will be a more appropriate choice.

Note: Functions can be written as follows:

function my_function(sequence name)
return "You are welcome " & name 
end function

What is a 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.

Syntax

routine_id(my_routine)

Parameters

  • my_routine: This is a sequence value, the name of the function or procedure whose routine_id is to be returned.

Return value

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 the routine_id() method is used. Otherwise, -1 will be returned.

  • We can indirectly call routine earlier than defined in the program.

Example

--define a couple of sample routines
function my_function(sequence name)
return "You are welcome " & name
end function
procedure my_favorite(sequence coding)
sequence favorite = "Your favorite of them is " & coding
printf(1,"%s", {favorite})
end procedure
--call first function
sequence name = "Miya"
printf(1,"%s \n",{my_function(name)})
--now using the routine_id() get the id these routines
atom id_number = routine_id("my_function")
atom id_favorite = routine_id("my_favorite")
--not defined yet we shall get the routine_id as well
atom id_hobby = routine_id("my_hobby")
-- print it before definition
printf(1,"%d \n",id_hobby)
--now define my_hobby method
function my_hobby(sequence coding,sequence sport)
return "You have two hubbies " & coding & " " & sport
end function
--printing the id_numbers to screen
print(1,id_number) -- output is 0
puts(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.

Explanation

  • 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.