Home / Highlights / FAQ / Examples / Quick Start / Download Forum / Register / Login

Function Parameters/Return and Strings

// You don't need to specify type of every parameter
// Multiple return values
example(int a, b, c) int, int { return a*b+c, a*b-c }
// b is a mutable in out variable
// c is uninitalized until written to. Note the peroid in .=
example2(int a, b mut, c out) int { b *= a; c .= 100; return 1 }
main()
{
	//Simple to assign multiple values
	a, b = example(5, 3, 2)
	assert(a == 17)
	assert(b == 13)
	print("$a $b")

	//clearly see what may be modified and overwritten
	int my_inout = 3
	returnValue = example2(2, my_inout mut, my_out out)
	print("$my_inout $my_out")
	assert(returnValue == 1)
	assert(my_inout == 6)
	assert(my_out == 100)
}
Output:
17 13
6 100

Error Handling

main() {
	value = fn1() error { return }
	print(value) //fn1 returns error so this is never executed
}

//The ! means it may be an error. InvalidParameter is predefined
fn2(int a) int! {
	if (a < 0 || a > 20)
		return InvalidParameter
	return a*2
}

fn1() int! {
	a = fn2(15) error {
		print("We shouldn't get an error here")
		//in the future you'll be able 
		//to assign to 'a' and not return
		return 1
	}
	print("A")

	//try can be used to propagate error
	//try fn2(-10)
	b = try fn2(3)

	print("B")

	c = fn2(30) error {
		print("C")
		return error //returns the error used to enter this block
	}	
	print("End of fn1")
	return 3
}
Output:
A
B
C

Array Bounds

GetArray(int size) int[] {
	//Next line is an alternative way to write int array[]
	array := $int[]
	for i = 0 to size {
		array.Push(100+i)
	}
	return array
}

GetInt(int v) int { return v*3 }

main() {
	for v in GetArray(0) {
		print(v) //Never executes
	}
	//_OnEmpty/_OnBreak/_OnComplete must follow the loop
	//like an else if must follow an if
	_OnEmpty  {
		print("Empty Array")
	}

	a = GetArray(10)
	//Uncomment either to get an array bounds error
	//print(a[0])
	//assert(a[0] == 100)
	
	//Several ways to solve this
	//One is by using literals
	if a.size >= 10
	{
		//array size is tagged as >= 10
		//9 is both >=0 and < 10
		assert(a[9] == 109)
	}
	
	//Another is a range check
	i = GetInt(1)
	if i >= 0 && i < a.size {
		assert(a[i] == 103)
	}

	//You can also do an inverted check
	if i < 0 {
		//i is tagged as a negative value
		return
	}
	//because the if returns the check is
	//inverted. i is now tagged as postive
	
	//Now an inverted check on size
	if i >= a.size //the = in >= is important
		return //return/break/continue needs no { }

	//i is now tagged as < a.size
	//i is in bounds as long as 'i' and 'a' isn't modified
	assert(a[i] == 103)
}
Output:
Empty Array