digital literacy for everyone



[lit] although fig 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/ in the section on [[conditionals]] we asked, wouldnt it be cool if instead of having to write the following code whenever we wanted to check if a file exists: try email = arropen "user/bobby/email.txt" now = "you have mail:" print now = email ; print except now = "you have no mail." ; print resume that any time you wanted to open a file, you could just use a command like: file = tryopen "email.txt" functions let you create such a command. now you may have noticed that fig has two types of command in regards to changing the variable on the left: now print this uses the value of now, but the print command doesnt change the value. now arropen "text.txt" this changes the value of now to hold the contents of text.txt the difference is that some commands change the value of the variable on the left, and some dont. print never changes a value, but arropen always does (if it runs successfully.) 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 fig uses the function command: function hi now "hello" print fig 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" function hi now "hello" return now fig 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 fig program or python 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. fig 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." to make it so that you can get functions the data it needs, and get the data you need back out, fig uses the return command-- to get data out-- and parameters, to get data in. fig has a fixed parameter count for each command. that means that arropen always takes one parameter-- a file path. it never takes fewer or more parameters. our hi function takes zero parameters but we can give it one: [fig] function hi what now randint 1 10 return now fig 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: function hi what now randint 1 what return now fig 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: function colour which p = which lcase ifequal p "black" now colortext 0 fig ifequal p "blue" now colortext 1 fig ifequal p "green" now colortext 2 fig ifequal p "teal" now colortext 3 fig ifequal p "red" now colortext 4 fig ifequal p "purple" now colortext 5 fig ifequal p "brown" now colortext 6 fig ifequal p "offwhite" now colortext 7 fig ifequal p "grey" now colortext 8 fig ifequal p "skyblue" now colortext 9 fig ifequal p "lime" now colortext 10 fig ifequal p "turquoise" now colortext 11 fig ifequal p "tomato" now colortext 12 fig ifequal p "pink" now colortext 13 fig ifequal p "yellow" now colortext 14 fig ifequal p "white" now colortext 15 fig fig 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. [fig] function colour 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 ifmore whichcolour -1 now colourtext whichcolour fig fig while now "name a colour to change the text to that colour, or q to quit - " prints ; lineinput ; colour now ; lcase ifequal now "q" break fig wend [img]functions.png[img] congratulations-- this is how programs work. sure, there could be output statements that change the output in a gui program instead of text, or get the input from a textbox instead of lineinput, but there are functions for doing all of that. you can even use the function command and inline python to create fig functions that interface with all kinds of program features: guis, more complex graphics features, network interfaces, or shell commands. actually, fig already has two features that access the shell: now "dir *.txt" shell #### list all the txt files in the current folder you can even use the shell command to open web browser tabs or a text editor to edit a file. function tryopen what try email = arropen what now = "you have mail:" print now = email ; return now except now = "you have no mail." ; print ; return "" resume fig file = tryopen "user/bobby/email.txt" if you want to write programs, teach yourself a few more fig 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. fig has a builtin command help feature you can run from the command line: help if you type "arr" it will show you all the commands that contain "arr" in them, what they do and which parameters they need. if you just hit enter, 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][url]
back to fig concepts: [url][url] fig main page: [url][url] home: [lit][lit]