the ~karx journals

various musings of ~karx

Sandwich Devlog 3: Function Junction

February 10, 2021 — ~karx

Today, I added functions! functions are a neat little tool that let you “pipe” math operation outputs into other instructions. For example, you could assign the output to a variable, like below:

#Declare a function
fxa3-3
#Call the function and assign it to variable
lv*x
#will print out 6
p$v

Or you could print it out directly:

#Declare a function
fxs30-3
#Call it and print it out
#Will print out 27
p*x

You can also use variables inside a function, like so:

#Declare a variable
lv3
#Declare a function that will use the variable
fxs30-$v
#Prints out 27
p*x

Note that functions will only get evaluated when they’re called. This means that if you reassign the variable v in between the function declaration and the function call, the function will use the new value of the variable. Also, functions are re-evaluated for each call. So if they depend on an outside variable and the variable changes frequently, the result of the function will also change frequently.

Nested function calls also work:

#Declare a function
fxa2-2
#Declare a function that calls the other one
fya*x-4
#Will print out 8
p*y

Also note that the only operations you can perform inside a function are the math functions, which are a, s, m, and d.

The code is up on tildegit, so clone a copy and get started with sandwich!

tags: sandwich, rust, programming, devlog