© Tyl Software Foundation 2019-2021
▶ FUNCTIONS
Function is an operative code section that gets parameters, does a job, and
optionally returns a value.
Function declaration looks like:
greet:
Tyl Symbolizer will replace ':' with '»', the function symbol:
greet »
Each function has a name. Here we declare the
Each function has its own list of statements, that are run upon function call. So let's add a statement:
greet
function. The symbol
'»
', is used as function symbol in Tyl programs, and comes at the end
of function signature.
Each function has its own list of statements, that are run upon function call. So let's add a statement:
greet:
print 'Hi'
While it is not mandatory, function statements are typically written with an
indentation regards to their enclosing function section.
If a function has only one statement, it is posible to write the statement right after the function symbol, in the same line. This is called a one line function:
If a function has only one statement, it is posible to write the statement right after the function symbol, in the same line. This is called a one line function:
greet: print 'Hi'
If a function has more than one statement, all function statements should be
written in the lines that come after the function declaration line:
greet:
print 'Hi'
print 'How is it going...'
Hi
How is it going...
How is it going...
If Tyl program has functions, after it runs
its statements it will run the first function in the program, provided that
it does not contain any parameter.
Let's say we call the first function 'go':
Let's say we call the first function 'go':
go:
print 'Hi'
go
and greet
functions:
go:
print 'Hi'
greet:
print 'Hi'
Functions can be called from another function. Let's say we want to call
greet
function from go
function, we would write:
go:
greet
greet:
print 'Hi'
Hi
The program starts from
If we want to greet someone by his name, we would add a function parameter:
go
function, and calls the greet
function.
When greet
function is called, it starts running its statements. The only
statement is print 'Hi'
, so the program will print 'Hi'.
If we want to greet someone by his name, we would add a function parameter:
greet someone:
The
To use
greet
function has one parameter 'someone'. We write the parameter right
after the function name.
To use
greet
function with someone
parameter:
go:
greet 'Oren'
greet someone:
print 'Hi ' + someone
Hi Oren
The line
An example with two parameters:
greet 'Oren'
, is calling the greet
function with the
string 'Oren', as the someone
parameter, thus when the first statement of
greet
function is executed, someone
parameter has the value
'Oren', and the program prints 'Hi Oren'
An example with two parameters:
go:
greet 'Oren' 'Origato'
greet name familyname:
print 'Hi ' + name + ' ' + familyname
Hi Oren Origato
Function can return a value:
pi: 3.1416
In Tyl, a function will always return the value or result of its last
executed statement. So
Using
pi
function will return the real number 3.1416
Using
pi
function:
go: print pi
pi: 3.1416
3.1416
Typically, static variables will be declared in the
module statements section of a program.
The last code can be changed to:
pi 3.1416
go: print pi
3.1416
Here's Perimeter and Area Calculator:
pi 3.1416
go:
rad 10
circleperimeter calccircleperimeter rad
circlearea calccirclearea rad
print circleperimeter + ', ' + circlearea
calccircleperimeter rad:
print 'calculating circle perimeter...'
2 * pi * rad
calccirclearea rad:
print 'calculating circle area...'
pi * rad * rad
calculating circle perimeter...
calculating circle area...
62.832, 314.16
calculating circle area...
62.832, 314.16