digital literacy for everyone


[lit]

[generate-title]

[lit] although quasi was designed to teach 7 programming concepts, these concepts are common or illustrative to some degree in many, if not all programming languages. learning these concepts will help you understand python, bash, javascript and many others. [[variables]] | [[input]] | [[output]] | [[basic-math]] | [[loops]] | [[conditionals]] | /functions/ functions let you create your own commands. you may have noticed that quasi has two types of command in regards to using the variable on the left: now print this uses the value of now, but the print command doesnt change the value. now plus "text" this changes the value of now to hold the contents of the string "text". the difference is that some commands change the value of the variable on the left, and some dont. *print* never changes a value, but *plus* always does (unless you add zero to a number or "" to a string.) in some languages, the non-changing commands are called "subroutines" or "subs" and the commands that change or "return" a value are called functions. python uses "def" to define both, and quasi uses the function command: hi function now "hello" print next now hi the first part of this code creates a function called hi, and the second part of the code "calls" or runs the function like any other command. once the function definition (the part at the top) is created, it can be called from anywhere in the program. now hi this command will not change the value of now, but if you want it to, it can. simply replace print in the function definition with "return now" hi function now "hello" return now next when you run the hi function, it will return the value of the now variable: p hi now the variable p is set to the return value of hi. the return value is set by the the return command. so p is set to "hello". thats how you get a value out of a newly-defined function. but why do we have to do that? when you write a quasi program, as with several other languages, you typically have to keep track of variable names. the more variable names you have, the more tedious it is to keep track of them. quasi and python make it easier, by treating the variables inside the function like a separate program. you can use all the variables you want inside the function-- they wont touch any values outside the function, even if they have the same name. this is called "local scope." an exception to this in quasiis when you make your own function (this is referred to as a "udf" or "user-defined function") and call a udf from a udf. user-defined have their own variables separate from the rest of the program, but not from each other. udfs share one set of variables. to make it so that you can get a function the data it needs, and get the data you need back out, quasi uses the *return* command-- to get data out-- and parameters, to get data in. quasi has a fixed parameter count for each command. that means that *left* always takes one parameter-- a length. it never takes fewer or more parameters. our hi function takes zero parameters but we can give it one: hi function what now randint 1 10 return now next now hi 10 we made our hi function return a random number from 1 to 10, so when we code now hi 10 thats what the variable now gets set to. but we also "pass" the what parameter, which we can use to get data into the function: hi function what now randint 1 what return now next now we can specify the largest random number we get back: now hi 20 print will give us a number between 1 and 20. sure, we could also just use randint with two parameters: now randint 1 20 print but if we are always going to have 1 as the lower number, we can create a shortcut to randint 1 20 with just one parameter: hi 20. parameters can be sent to the function whether it returns a value or not. remember our colour table from the [[output]] section? black = 0 blue = 1 green = 2 teal = 3 red = 4 purple = 5 brown = 6 offwhite = 7 grey = 8 skyblue = 9 lime = 10 turquoise = 11 tomato = 12 pink = 13 yellow = 14 white = 15 if we want, we can create a version of this that takes a string parameter and lets the user change the colour based on input: colourname function which p = which lcase now ifequal p "black" now colour 0 next now ifequal p "blue" now colour 1 next now ifequal p "green" now colour 2 next now ifequal p "teal" now colour 3 next now ifequal p "red" now colour 4 next now ifequal p "purple" now colour 5 next now ifequal p "brown" now colour 6 next now ifequal p "offwhite" now colour 7 next now ifequal p "grey" now colour 8 next now ifequal p "skyblue" now colour 9 next now ifequal p "lime" now colour 10 next now ifequal p "turquoise" now colour 11 next now ifequal p "tomato" now colour 12 next now ifequal p "pink" now colour 13 next now ifequal p "yellow" now colour 14 next now ifequal p "white" now colour 15 next next thats a lot of ifequal statements. is there a better way to do this? actually, there is. first, we can setup an array using the split command: textcolours = "black blue green teal red purple brown offwhite grey skyblue lime turquoise tomato pink yellow white" split textcolours " " just like that, we have an array named textcolours where item 1 is black and 2 is blue, etc. we can use the instr function (it is like a search feature) to turn the array item into a number: whichcolour = instr textcolours which that will give us 0 for "not found" and 1 for black and 2 for blue... but we want 0 for black and 1 for blue: whichcolour = instr textcolours which minus 1 now we will get -1 for "not found" and 0 for black and 1 for blue, up to 15 for white. colourname function which textcolours = "black blue green teal red purple brown offwhite grey skyblue lime turquoise tomato pink yellow white" split textcolours " " whichcolour = instr textcolours which minus 1 now ifmore whichcolour -1 now colour whichcolour next next now "this text is brown" ; colourname "brown" ; prints [img]functions.png[img] congratulations-- this is how programs work. if you want to write programs, teach yourself a few more quasi commands. practice the ones youve learned. they all work more or less like the ones demonstrated in these 7 sections: [[variables]] | [[input]] | [[output]] | [[basic-math]] | [[loops]] | [[conditionals]] | /functions/ dont be afraid to review-- you learn these commands by using them. quasi has a builtin command help feature: it will list all commands. dont just learn by writing programs. thats a good way, but be sure to take existing examples, play around with them, make changes, try things and ask yourself: "what will happen if i change this?" make your best guess. then run it. if it doesnt do what you want, try something else, look up the command again, search online for more information. thats how you get better at coding. if you have questions, you can ask on this forum: [url]http://softwarefreedom.jcink.net/[url]
back to quasi concepts: [url]https://codeinfig.neocities.org/quasiconcepts/index.html[url] quasi main page: [url]https://codeinfig.neocities.org/quasi.html[url] home: [lit]https://codeinfig.neocities.org[lit]