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
#Call the function and assign it to variable
#will print out 6

Or you could print it out directly:

#Declare a function
#Call it and print it out
#Will print out 27

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

#Declare a variable
#Declare a function that will use the variable
#Prints out 27

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
#Declare a function that calls the other one
#Will print out 8

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