Functions
This page is still under construction!
Functions are simple ways to define subroutines or common and repetitive tasks.
The basic concept of a function is a block of code that can maybe accept arguments and can maybe return data. This block can easily be called by other functions in any moment or order.
Declaring and Invoking
A example of function declaration and call in tq is
# Declaring a function called `foo`
func foo() {
Std.Console.writeln("Hello, World!")
}
func main() !void {
Std.Console.writeln("foo will be invoked now...")
# Invoking our foo function
foo()
Std.Console.writeln("foo finished!")
}foo will be invoked now...
Hello, World!
foo finished!
the syntax for declarate a function is:
func <name>(<argument type> <argument name>, <...>) <optional type> { <code to execute> }
where <name> is the identifier for reffer to the function and
<optional type> is the type that the function returns, ornothing for void.
A function can be invoked by it reference followed by a opening and closing parenthesis () as follows:
myFunctionReference()Parameters and arguments
Some functions can ask for arguments to return some operated value or behave diferently depending on how it is needed.
During the function declaration, parameters can be included as typed identifiers to identify their type and allow them to be refered inside the function block.
# the function `foo` will ask for 2 arguments to be invoked.
# - The first can be a value of any type that will be refered as `any`.
# - The seccond is a 32 bit integer that will be refered as `number`.
func foo(anytype any, i32 number) void { ... }If the function ask for arguments, the values should be declarated, in order, inside the parenthesis
@public func foo(i32 a, i8 b, i128 c) void { ... }
func ... {
const i32 myInt = 100
const myByte = 255
const i128 myLonger = 2 ** 100
# correct call!
foo(myInt, myByte, myLonger)
There's no function 'foo' with this parameter type order!
foo(myByte, myInt, myLonger)
There's no function 'foo' that accepts only a byte!
foo(myByte)
}Function Overloading
Two or more functions can have the same identifier if they have different types. When a function is declarated with the same name, but with a diferent tipe, it's called overload.
The tq Language emphasizes code readability and understanding over abstraction. As a good practice, make sure to only use function overloading when the result will be the same for the same equivalent values.
Examples
# Different results are named diferently
func writeText(string value) {
Std.Console.writeln("My string is: " + value)
}
func writeText([]char value) {
Std.Console.writeln("My string is: " + string.join(value))
}
func writeNumber(i32 value) {
Std.Console.writeln("My number is: " + value)
}# Different results with the same name can result in a
# harder understanding of the code
func writeText(string value) {
Std.Console.writeln("My string is:" + value)
}
func writeText([]char value) {
Std.Console.writeln("My string is:" + string.join(value))
}
func writeText(i32 value) {
Std.Console.writeln("My number is:" + value)
}