Function

A function value is a callable block of code. Functions are first-class citizens in Sparkle, they can be assigned to variables, passed as arguments to other functions, and returned from functions.

let greet be function with just name as
  print "hello {name}"

call greet with just "world"

Definition

An empty function can be defined using function as the value. By default a function returns blank.

let empty_function be function

A block of code can be defined for the function using as, then called using call.

let hello_world be function as
  print "hello world"

call hello_world

A function may have parameters defined using with ... and following the Oxford comma rule.

let add be function with a, and b as
  return a plus b

print call add with 1, and 2

If the function definition or call only uses one parameter, use with just ... instead.

let greet be function with just name as
  return print "hello {name}"

call greet with just "world"

There's an implicit return statement at the end of a function if it doesn't return a value.

let hello be function as
  print "hello world"

let x be call hello
print x # blank

Function parameters can be made optional by assigning a default value using as. Optional parameters may only be added after all required parameters.

let incremented be function with n, and amount as 1 as
  return n plus amount

print call incremented with just 100 # prints 101

Methods

Returns "function"

let x be function with a, and b as a plus b
print x # function