Intro
A Kin function is a block of code designed to perform a particular task.
A Kin function is executed when "something" invokes it (calls it).
Ex
porogaramu_ntoya multiply(a: umubare, b: umubare): umubare {
tanga a * b
}Function Syntax
A Kin function is defined with the porogaramu_ntoya keyword, followed by a name, followed by parentheses ().
Function names can contain letters, digits, and underscores (same rules as variables). They must start with a letter or _. $ is not allowed.
The parentheses may include parameters separated by commas. Each parameter may
have a type annotation. After ) you may write a return type:
porogaramu_ntoya name(parameter1: umubare, parameter2: ijambo): ijambo {
# code to be executed
}Function parameters are listed inside the parentheses () in the function definition.
Function arguments are the values received by the function when it is invoked.
If a parameter is annotated, Kin checks the argument type at call time.
If a return type is annotated, Kin checks the value from tanga.
Inside the function, the arguments (the parameters) behave as local variables.
Function Invocation
The code inside the function will execute when "something" invokes (calls) the function:
functionName(argument1, argument2, argument3, ...)Function Return (tanga)
When Kin reaches a tanga statement, the function stops executing. Do not write return — the keyword is tanga.
If the function was invoked from a statement, Kin will return to execute the code after the invoking statement.
Functions often compute a return value. That value is given back to the caller:
porogaramu_ntoya myFunction(a: umubare, b: umubare): umubare {
# Function returns the product of a and b
tanga a * b
}
# Function is called, the return value will end up in x
reka x: umubare = myFunction(4, 3)Statements after
tangain the same function are not executed.tangaleaves the function immediately.
So this does not print anything extra (the tangaza_amakuru never runs):
porogaramu_ntoya function(a, b) {
tanga a*b
tangaza_amakuru("Product is ", a*b)
}Why Functions?
With functions you can reuse code
You can write code that can be used many times.
You can use the same code with different arguments, to produce different results.