Tyl Programming Language
▶ FUNCTIONS
Function is an operative code block that gets parameters, does a job, and optionally returns a value.
Function declaration looks like:
greet »
Each function has a name. Here we declare the function greet. The symbol: '»', is used as the function symbol in Tyl programs, and comes after the function name (and optionally - function parameters).

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, statements are typically written with an indentation regards to their enclosing code block.

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 must 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...
Each Tyl program must have a function with the name: 'main', that is used as the main entry point of the program. When the Tyl program runs, it starts to run the statements in the 'main' function.

Let's look at the main function:
main »
print 'hi'
main and greet functions:
greet »
print 'hi'

main »
print 'hi'
Functions can be activated from another function. Let's say we want to activate the greet function from main function, we would write:
greet »
print 'hi'

main »
greet
hi
The program starts from main 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 greet function has one parameter: 'someone'. We write the parameter right after the function name

To use the greet function with the someone parameter:
greet someone »
print 'hi ' + someone

main »
greet 'Oren'
hi Oren
The line: 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:
greet name family_name »
print 'hi ' + name + ' ' + family_name

main »
greet 'Oren' 'Origato'
hi Oren Origato
Function can return a value:
pi » 3.1416
In Tyl, a function will always return the value of its last statement. So pi function will return the real number: 3.1416

Using pi function:
pi » 3.1416

main »
print pi
3.1416
Typically, static variables will be declared in the first statements of a program.

The last code will be changed to:
pi 3.1416

main »
print pi
3.1416
Here's Perimeter and Area Calculator:
pi 3.1416

calc_circle_perimeter rad »
print 'calculating circle perimeter...'
2 * pi * rad

calc_circle_area rad »
print 'calculating circle area...'
 pi * rad * rad

main »
 rad 10
 circle_perimeter calc_circle_perimeter rad
 circle_area calc_circle_area rad
print circle_perimeter + ', ' + circle_area

62.83, 314.16