To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Official ZPE/YASS documentationFunctions and parameters

YASS
function main($args)
  //Do some stuff here
end function
  

Functions became a huge part of YASS in version 1.3.4 when they became importable using the import function (and later the import feature). This meant that compiled scripts could be imported into the current script.

Since version 1.4.1, functions can be given a scope. A scope can be normal, private or public. The previous example showed a function with the normal scope. This is roughly equivalent to private. Private means that the function may not be accessed in any program other than it's own. Public allows other scripts or programs to access the function.

Because of the powerful way in which ZPE handles this, the import command is extremely useful for importing existing, compiled scripts.

The following sample demonstrates three functions, each with a different scope.

YASS
function main($args)
  //This function has the normal scope
end function
private function main($args)
  //This function has the private scope
end function
public function main($args)
  //This function has the public scope
end function
  

Nested functions

Many languages allow the use of nested functions. Since functions are first-class citizens, they also allow for encapsulation of internal methods. In this case, a function may itself contain a function:

YASS
function main($args)
  function say_name()
    print("Hello there Jamie")
  end function

  say_name()
  other_function()
end function

function other_function()
  //This will throw a ZPE WARNING as it cannot find the say_name function
  say_name()
end function

Lambda functions

As of version 1.3.7.44 lambda (λ) expressions have been supported in ZPE. These are known as anonymous functions and they are designed to make the language more flexible and simpler. They are, without any doubt, my favourite feature of the language and you will see that a lot of my functions benefit from the use of lambda functions.

A lambda function or lambda abstraction is a function that is not bound to any identifier, that is, the function is never defined as a function but it can be assigned to a variable and so on. ZPE version 1.3.7.44 added support for lambda expressions.

The syntax is shown below:

YASS
$foo = function($x, $y, $z){return $x + $y + $z}
$bar = function($x, $y, $z) return $x + $y + $z end function
print($foo(3, 4, 6))
print($bar(3, 4, 6))
  

The language provides two ways of defining these functions, one is the C-like way of using curly braces ({}) around the function and the second is to use the end function keywords to terminate the function.

Functions need not have a return value, that is, no return function is required in order for this to work:

YASS
$foo = function($x){print ($x + 5)}
$foo(3)
  

Lambda expressions also can be given values straight from the declaration, which can make them more useful in other functions:

YASS
$v = 10
print(function($x){return ($x + 5)}($v))

The result of this evaluation is 10, so the print function will simply print 10. Variables, functions and so on can all be used as well.

Lambda functions are also very useful with the special csv_decode function, which takes a function as it's optional second parameter. Rather than defining some function that may only be used once and then calling the handle_of function on that function, it may make more sense to define a lambda function:

YASS
$content = "32, 41, 91, 36"
$out = csv_decode($content, function($x){return $x + 5})
//Should print 37, 46, 96, 41
print($out)
$out = csv_decode($content, function($x, $rowpos, $columnpos){
  return $x + 5 + $columnpos
})
//Should print 37, 47, 98, 44 because the column position is added to the value
print($out)
  

Also note, that unlike Python, lambda functions can be defined across many lines, not just one.

The previous example could also be rewritten to store a lambda function in a variable an call it using it's variable reference:

YASS
$content = "32, 41, 91, 36"
$out = csv_decode($content, function($x){return $x + 5})
//Should print 37, 46, 96, 41
print($out)

$csv_func =
function($x, $rowpos, $columnpos)
  return $x + 5 + $columnpos
end function

$out = csv_decode($content, $csv_func)

//Should print 37, 47, 98, 44 because the column position is added to the value
print($out)
  

It is important to note that ordering of lambda variables is important. For a function like csv_decode the order is crucial but it can be any number of parameters so for instance $columnpos need not be specified.

Also, parameter names do not matter either since when the csv_decode function is run it allocates values to the parameters from left to right, not based on names.

Short-hand lambda functions

Version 1.6.7 introduces an alternative syntax similar to JavaScript that allows anonymous functions to be defined with the fat arrow (=>):

YASS
$foo = ($x, $y, $z) => {return $x + $y + $z}
$bar = ($x, $y, $z) => return $x + $y + $z end function
  

Optional parameters

Any parameter can be optional, the only thing that must be done after that is to determine whether it has been set or not. The is_set predefined function is very useful for this purpose.

In some senses, optional parameters was a side effect of the main development of ZPE. It is useful and remains as an option in all versions.

YASS
function test($arg1, $arg2, $arg3)
  if(is_set($arg2) && is_set($arg3))
    print($arg2)
    print($arg3)
  end if
end function
function main ()
  test(10)
  test(10, 20)
  test(10, 20, 30)
end function
  

Default values

A default value is somewhat of a fall back if a parameter is not given an argument. This only works with non-named parameters. Default values were officially supported (although added much earlier the implementation wasn't finished) from ZPE version 1.8.9.

YASS
function test($arg1, $arg2, $arg3 = 90)
  print($arg1, $arg2, $arg3)
end function
function main ()
  test(10, 20)
end function
  

Infinite parameters

ZPE 1.8.11 (North, released in October 2020) added support for infinite parameters. This feature is very powerful and allows programmers to write just one parameter followed by the double dot (..) rather than specifying parameters.

Further to this, infinite parameters are numbered automatically.

YASS
function main()

  infiniteParameters(10, 20, 30)

end function

function infiniteParameters($i .. )

  print($i0)
  print($i1)
  print($i2)
  print($i)

end function

In this example, the variable $i is an infinite parameter list. Notice that this creates $i0, $i1 and $i2 as three arguments have been given. The $i variable will contain a list of all infinite parameters.

A really good example comes from my Standard Library for YASS - a project dedicated to showcasing the features of the YASS language. This next example creates an addAll function (based on the infinite_product_calculator function in the Standard Library) with infinite parameters:

YASS
function main()

  print(addAll(10, 20, 30))

end function

function addAll($n ..)
  $total = 0
  $len = list_get_length($n)
  for($i = 0 to $len)
    $total = $total + $n[$i]
  end for

  return $total

end function
    
Comments

There are no comments on this page.

New comment
Reply
Feedback 👍
Comments are sent via email to me.