Functions
+Defining Functions
+In Solstice, functions are a way to organise and reuse code. Here’s an example function:
+def add(int a, int b) int {
+ return a + b
+}
+
+Here’s what each part is:
+-
+
def: Means “define”. Tells Solstice we are defining a function.
+add: This is your function name! Choose something unique.
+(int a, int b): These are the arguments your function takes. +-
+
- Enclose your argument type and name pairs in brackets. +
- The first identifier should be a type (such as
int,string, etc), the second should be an identifer
+ - After each type-name pair, add a comma unless you’ve got no more arguments to define +
+int: This is your return type
+return a + b: This is where your code goes for the function
+
Calling Functions
+Call a function in Solstice like this:
+myFunction(a, b, c)
+
+where:
+-
+
myFunctionis the name of your function
+(a, b, c)are the arguments you would like to pass to the function. +-
+
- Enclose the arguments in parentheses (
( ))
+ - Each argument should be seperated by a comma +
- Solstice will type-check each argument to ensure it matches the function’s specified types +
+- Enclose the arguments in parentheses (
Closures and Scoping
+Each function has an attached closure, which is the state captured at runtime when the function is defined. All variables in the closure can be accessed inside the function body, however the closure cannot be modified (meaning all variables captured remain consistent across function runs).
+This behaviour ensures that functions are not affected by global state, and will always have reliable, consistent behaviour.
+Additionally, all defined arguments are avaliable in scope.
+x = 10
+
+def myFunction() int {
+ // Here, x is avaliable, however if we modify it,
+ // our changes won't affect the external state, or
+ // the state of this function when it runs.
+ puts x
+ x = 5
+ return x
+}
+
+myFunction() // returns 5, prints 10
+myFunction() // returns 5, prints 10
+
+puts x // prints 10, x has not been modified by myFunction
+
+The type of a function
+If you’d like to accept a function as an argument, you need to specify it’s type signature. That can be done like this:
+fun(int, string) bool
+
+where:
+-
+
funtells Solstice we’re writing a type signature for a function
+(int, string)is the list of argument types (names are not needed), seperated by commas, surrounded by brackets
+boolis the return type
+
making this type represent a function which takes an int and a string, and returns a bool.
Introduction
Welcome to the Solstice documentation! Here we have a tutorial (starting here) and a specification for the Solstice programming language.
If you’re new to Solstice, I’d recommend starting the tutorial, at the installation page. If you’ve already installed Solstice, start at the Basics page.
+Found a mistake? Report it at https://chookspace.com/solstice/docs
Docs generated by mdBook.
@@ -208,22 +209,6 @@ - - - +Introduction
Welcome to the Solstice documentation! Here we have a tutorial (starting here) and a specification for the Solstice programming language.
If you’re new to Solstice, I’d recommend starting the tutorial, at the installation page. If you’ve already installed Solstice, start at the Basics page.
+Found a mistake? Report it at https://chookspace.com/solstice/docs
Docs generated by mdBook.
@@ -208,22 +209,6 @@ - - - +Introduction
Welcome to the Solstice documentation! Here we have a tutorial (starting here) and a specification for the Solstice programming language.
If you’re new to Solstice, I’d recommend starting the tutorial, at the installation page. If you’ve already installed Solstice, start at the Basics page.
+Found a mistake? Report it at https://chookspace.com/solstice/docs
Docs generated by mdBook.
Installation
@@ -325,7 +326,7 @@ Args: -We’ll look at types of values in the next part.
+We’ll look at types of values soon.
Recalling Variables
Recall a variable’s content by using it’s name:
puts name
@@ -399,6 +400,72 @@ false
Solstice has three other types: fun, template, and object, but we’ll cover these in the Functions and Structures pages.
Functions
+Defining Functions
+In Solstice, functions are a way to organise and reuse code. Here’s an example function:
+def add(int a, int b) int {
+ return a + b
+}
+
+Here’s what each part is:
+
+def: Means “define”. Tells Solstice we are defining a function.
+add: This is your function name! Choose something unique.
+(int a, int b): These are the arguments your function takes.
+
+- Enclose your argument type and name pairs in brackets.
+- The first identifier should be a type (such as
int, string, etc), the second should be an identifer
+- After each type-name pair, add a comma unless you’ve got no more arguments to define
+
+
+int: This is your return type
+return a + b: This is where your code goes for the function
+
+Calling Functions
+Call a function in Solstice like this:
+myFunction(a, b, c)
+
+where:
+
+myFunction is the name of your function
+(a, b, c) are the arguments you would like to pass to the function.
+
+- Enclose the arguments in parentheses (
( ))
+- Each argument should be seperated by a comma
+- Solstice will type-check each argument to ensure it matches the function’s specified types
+
+
+
+Closures and Scoping
+Each function has an attached closure, which is the state captured at runtime when the function is defined. All variables in the closure can be accessed inside the function body, however the closure cannot be modified (meaning all variables captured remain consistent across function runs).
+This behaviour ensures that functions are not affected by global state, and will always have reliable, consistent behaviour.
+Additionally, all defined arguments are avaliable in scope.
+x = 10
+
+def myFunction() int {
+ // Here, x is avaliable, however if we modify it,
+ // our changes won't affect the external state, or
+ // the state of this function when it runs.
+ puts x
+ x = 5
+ return x
+}
+
+myFunction() // returns 5, prints 10
+myFunction() // returns 5, prints 10
+
+puts x // prints 10, x has not been modified by myFunction
+
+The type of a function
+If you’d like to accept a function as an argument, you need to specify it’s type signature. That can be done like this:
+fun(int, string) bool
+
+where:
+
+fun tells Solstice we’re writing a type signature for a function
+(int, string) is the list of argument types (names are not needed), seperated by commas, surrounded by brackets
+bool is the return type
+
+making this type represent a function which takes an int and a string, and returns a bool.
Structures
@@ -430,6 +497,10 @@ new
as
sizeof
pragma
+
+fun
+template
+object
Comments
@@ -1311,22 +1382,6 @@ use "parser/parser" - - - +We’ll look at types of values in the next part.
+We’ll look at types of values soon.
Recalling Variables
Recall a variable’s content by using it’s name:
puts name
@@ -238,22 +238,6 @@
-
-
-
+
@@ -272,22 +272,6 @@
-
-
-
+
@@ -251,22 +251,6 @@ false
-
-
-
+
@@ -220,22 +220,6 @@
-
-
-
+
@@ -240,22 +240,6 @@
-
-
-
+
@@ -239,22 +239,6 @@
-
-