Table of Contents
A guide to programming in Funk
Welcome to Funk! Funk is a high level, object oriented programming language, similar to Python or JavaScript. It's pretty fast, and has a growing set of features for you to use.
Let's go!
I'm assuming you've got the following:
- The Funk interpreter (steps for getting this are at the main page)
- A text editor
- A terminal
Once you've got those, we can write our first program in Funk.
This is what a Hello, world! program looks like:
main func {
println("Hello, world!")
}
This should be familiar to most experienced programmers, but here's a quick breakdown on how this works:
main funcdefines your "main" function, which is where the program starts executing. Funk scans your code file for all the functions and variables you define before running any code, to make programming slightly easier.- Anything inside the curly braces (
{and}) is inside the "code block" of the main function. All the code inside the code block of the main function is run when your program starts. println("Hello, world")is a call to theprintlnfunction, which is avaliable by default in Funk. This will print whatever arguments you pass to the function (in this case, "Hello, World!") to the terminal, adding a new line at the end.
If you've got the Funk interpreter installed, you can run funk (name of program).funk to make the program go!
Writing Useful Programs
Funk has a growing set of features, and you'll probably want to use some of them. Just printing different messages to the screen gets boring after a while. So here's some features that you'll find in other programming languages that you may find useful.
P.S. If you want to comment out a line of code (make the interpreter ignore it), just add //, and then the message you'd like your comment to contain.
Variables
Variables contain a value of some sort. Here are some kinds values that variables can contain:
- Integers (7, 42, 4372789432)
- Floating point numbers (3.14, 32.123)
- Strings of characters ("Hello, world!", "dingus")
- Boolean values (either true or false)
- Lists (CURRENTLY IN DEVELOPMENT) (a collection of other values, like [32, "Hi there!", true])
- Functions (like your main function)
Set a variable with the syntax name = value, similar to Python. For example:
main func {
// Set the variable named 'x' to 5
x = 5
// Print the contents of 'x'
println(x)
// You can do this for all the kinds of values
myInt = 7
myFloat = 3.141
myBool = true
myString = "dingus"
myList = ["Hi there", 3212, false]
myFunction = func {
println("Hello from my function!")
}
}
if and while
Two inbuilt functions for Funk are if and while statements. These allow you to execute code, only if some conditions are true.
For example, have a look at this code:
main func {
x = 7
// This code will run, since x is 7
if (x == 7) {
println("x is equal to 7!")
}
// But this code won't
if (x == 5) {
println("x is equal to 5!")
}
}
This code should look familiar to most programmers.
But what if we want to make a loop? Enter while. This will continually run the code in the loop until the condition is false.
main func {
x = 0
while (x < 10) {
x += 1
println(x)
}
}
This code will count from 1 to 10.
Math
Funk supports simple math. You can use operations +, -, *, and /.
main func {
println(1 + 1) // prints 2
}
You can also use +=, -=, *=, and /= to operate on variables.
main func {
x = 1
println(x) // prints 1
x += 1
println(x) // prints 2
}
Functions
Functions in Funk work like many other programming languages. You can define them and call them.
// Define a function
myFunction func {
println("The function got called!")
}
main func {
// Call the function
myFunction()
}
Argument passing and returning are under active modification, but for now arguments passed to a function are accessible as arg0, arg1, arg2, and so on.
Functions in Funk are first class, so you can do things like this:
main func {
silly(silly)
}
silly func {
arg0(arg0)
}
This will recursively call the function passed to silly over and over.