Tcl - Built-in Functions

Tcl provides a number of built-in functions (procedures) for various operations. This includes −
Functions for list handling.
Functions for list handling.
Functions for string handling.
Functions for string handling.
Functions for array handling.
Functions for array handling.
Functions for dictionary handling.
Functions for dictionary handling.
Functions for File I/O handling.
Functions for File I/O handling.
Functions for creating namespaces and packages.
Functions for creating namespaces and packages.
Functions for Math operations.
Functions for Math operations.
Functions for System operations.
Functions for System operations.
Each of the above except for math and system functions are covered in earlier chapters. Math and system built-in functions are explained below.
The math functions available in Tcl are listed in the following table −
abs arg
Calculates the absolute value of arg.
acos arg
Calculates the arccosine of arg.
asin arg
Calculates the arcsine of arg.
atan arg
Calculates the arctangent of arg.
atan2 y x
Calculates the arctangent of the quotient of its arguments(y/x).
ceil arg
Calculates the smallest integer greater than or equal to a number.
cos arg
Calculates the cosine of arg.
cosh arg
Calculates the hyperbolic cosine of arg.
double arg
Calculates if arg is a floating-point value, returns arg, otherwise converts arg to floating-point and returns the converted value.
exp arg
Calculates an exponential function (e raised to the power of arg).
floor arg
Calculates the largest integer less than or equal to arg.
fmod x y
Calculates the floating-point remainder of the division of x by y. If y is 0, an error is returned.
hypot x y
Calculates the length of the hypotenuse of a right-angled triangle sqrt(x*x+y*y).
int arg
Calculates if arg is an integer value of the same width as the machine word, returns arg, otherwise converts arg to an integer.
log arg
Calculates the natural logarithm of arg.
log10 arg
Calculates the base 10 logarithm of arg.
pow x y
Calculates the value of x raised to the power y. If x is negative, y must be an integer value.
rand
Calculates a pseudo-random number between 0 and 1.
round arg
Calculates the value of arg rounded to the nearest integer.
sin arg
Calculates the sine of arg.
sinh arg
Calculates the hyperbolic sine of arg.
sqrt arg
Calculates the square root of arg. arg must be positive.
srand arg
Calculates a pseudo-random number between 0 and 1. The arg, which must be an integer, is used to reset the seed for the random number generator of rand.
tan arg
Calculates the tangent of arg.
tanh arg
Calculates the hyperbolic tangent of arg.
wide arg
Calculates integer value at least 64-bits wide (by sign-extension if arg is a 32-bit number) for arg if it is not one already.
Some examples using math functions are given below −
#!/usr/bin/tclsh

namespace import ::tcl::mathfunc::*
puts [tan 10]
puts [pow 10 2]
puts [ceil 10.34]
puts [hypot 10 20]
puts [srand 45]
puts [log 10]
puts [srand 45]
When the above code is executed, it produces the following result −
0.6483608274590866
100.0
11.0
22.360679774997898
0.0003521866166741525
2.302585092994046
0.0003521866166741525

The important system functions in Tcl includes,
clock − seconds function, which returns current time in seconds.
clock − seconds function, which returns current time in seconds.
clock − format function, which formats the seconds into date and time.
clock − format function, which formats the seconds into date and time.
clock − scan function, which scans the input string and converts it into seconds.
clock − scan function, which scans the input string and converts it into seconds.
open − function, which is used to open a file.
open − function, which is used to open a file.
exec − function, which is used to execute a system command.
exec − function, which is used to execute a system command.
close − function, which is used to close a file.
close − function, which is used to close a file.
Some examples for the above functions are listed below −
#!/usr/bin/tclsh

#get seconds
set currentTime [clock seconds]
puts $currentTime
#get format 
puts "The time is: [clock format $currentTime -format %H:%M:%S]"
puts "The date is: [clock format $currentTime -format %D]"

set date "Jun 15, 2014"
puts [clock scan $date -format {%b %d, %Y}]

puts [exec ls]
puts [exec dir]

set a  [open input.txt]
puts [read $a];
puts $a
close $a
When the above code is executed, it produces the following result −
1402819756
The time is: 03:09:16
The date is: 06/15/2014
1402808400
input.txt
main.tcl
input.txt  main.tcl
This is the file you can use to provide input to your program and later on open
   it inside your program to process the input.

file3

The following table provides the list strings that can be used to format the date and time.
%a
Day in short form, eg:Sun.
%A
Day in full form eg:Sunday.
%b
Month in short form.
%B
Month in full form.
%d
Day of month.
%j
Julian day of year.
%m
Month in number.
%y
Year in two digits.
%Y
Year in four digits.
%H
Hour in 24 hour clock.
%I
Hour in 12 hour clock.
%M
Minutes.
%S
Seconds.
%p
AM or PM.
%D
Date in number, mm /dd/yy.
%r
Time in 12 hour clock.
%R 
Time in 24 hour clock without seconds.
%T
Time in 24 hour clock with seconds.
%Z
Time Zone Name like GMT, IST, EST and so on.
 Print
 Add Notes
 Bookmark this page