What is the printf() method in Euphoria?
Overview
A good number of methods exist in Euphoria that can be used to display information to output. However, there is one method that is usually favored among the rest, the printf() method. This method is used to print formatted strings to the display.
A formatted string will contain a variable placeholder in it that will be replaced with the value of such a variable at the run time.
Syntax
printf(fn,format,valueToPrint)
Parameter
fn: This parameter indicates the file or device number to write to. For files, it is always1when the file is found or available as indicated by the open function. It is an integer value.format: This holds the text to print, if any, and a format specifier, which will be replaced by thevalueToPrintparameter. It is a double quoted sequence.valueToPrint: The variable whose value will be used in place of the format specifier. It is of thetypeobject, which implies it can be of any data type.
Return value
The return value is an object.
Code
In the code snippet below, a sample illustration of how the printf() method can be used is shown.
--define some atom variablesatom age =30, height = 5.7--define a sequencesequence name = "Mr Kyle"-- print a formated string to displayprintf(1,"I have a neighbor, %s, he is %.1f tall and is %d years young",{name,height,age} )
Explanation
- Line 3: We define the atom variables
ageandheight. - Line 6: We define a string name.
- Line 9: We call the
printf()method. The first parameter passed to theprintf()method is1, which is the value of the standard output. The next parameter is a formatted string, which contains some format specifiers:%s: To be replaced by the value of the sequencename.%.1f: This specifies that the height value should be displayed with one decimal place and lastly.%d: This will be replaced by the integer atomage. The last parameter passed to the function is the variable whose value will be substituted into the formatted string. They are placed in a sequence because they are multiple and are arranged in order of appearance in the formatted string.