Learn
Functions
Functions package reusable behavior into named pieces of code.
Define a function
functions.tk
fn add(a, b):
return a + b
print(add(5, 3))Declare functions and methods with fn.
Return values
return.tk
fn square(x):
return x * x
result = square(8)
print(result)Default arguments
defaults.tk
fn welcome(name, greeting = "Hello"):
print(greeting + ", " + name)
welcome("Ayaan")
welcome("Ayaan", "Hi")Local values
Function parameters and values created inside a function are local to that call. Pass data into functions explicitly when possible.
scope.tk
fn double(value):
result = value * 2
return result
print(double(21))Methods
Methods use the same fn declaration as other functions and receive self explicitly.
method.tk
class Counter:
fn __init__(self, start=0):
self.value = start
fn increment(self):
self.value += 1
return self.value