if youre just learning to code, functions can be intimidating and you may even wonder what theyre good for. i avoided them for years, and wish id found the right appeal to encourage me to give them a chance.
- the importance of functions is that they help you organize and write better code.
- the fun of functions is that they let you create your own commands.
- the practicality of functions is that they keep you from having to come up with as many unique names for variables.
- functional languages use them to make code more efficient and concurrent.
without knowing what functions are good for, they could seem tedious. but for what they do, theyre very simple.
first of all, a bit on terminology: i refer to a function as any self-contained subroutine with a name. now whats a subroutine? a subroutine is to your program what a paragraph is to a book; its a smaller section of code that deserves its own unit (and everything in it is related.)
that means that as far as im concerned, your print command is a function, chr() is a function: any command.
some languages distinguish between functions and subs. whats the difference? a function is like a sub but returns a value. phooey! in python (and fig) a function doesnt have to return a value, so we dont try distinguishing. also the term “subroutine” is much broader than some languages make it out to be, so lets not bother with that.
here is your function… it takes a number and multiplies it by 4:
function timesfour(x): z = x ; times 4 ; return z fig
in python its very similar, except for the fig command (which is the same as unindenting in python, so actual indentation in fig is ignored) and the punctuation is required: (its optional in fig)
def timesfour(x): z = x * 4 ; return z
in python you could– but probably shouldnt– do this as a one-liner: (only because it probably cuts down readability)
def timesfour(x): return x * 4
now, once weve made our user-defined function, we have to use it in the program. actually we dont have to, but why else would it be there?
p = 7 ; timesfour p ; print another = 8 ; timesfour another ; print zebra = 9 ; timesfour zebra ; timesfour zebra ; print
p = 7 ; print timesfour(p) another = 8 ; print timesfour(another) zebra = 9 ; zebra = timesfour(zebra) ; zebra = timesfour(zebra) ; print zebra # or: print timesfour(timesfour(9))
so you can see how you can make and use your own functions. remember that functions in fig and python dont have to return a value? simply dont use return:
function printagain x z = x ; print ; print fig
def printagain(x): print x ; print x
you can call it the same way as a “true function” in python or fig, but in python now you can call it like this if you prefer:
printagain("hello") # print twice. also works on variables.
why put things in functions? aside from the organizational advantages, and that you dont have to repeat often-used code (or hide it in a giant loop) any variables inside the function are “local in scope.”
what does that mean? it means that if you have a variable x outside the function, and you have a variable with the same name inside the function, the function will not tamper with the value of x on the outside.
which is great: variables / logic / code inside a function dont have to worry about the current value or disturbing the current value of things. you can pass one or more “parameters” into the function on the top line:
function newprint x y zebra z = zebra ; print z = x ; print z = y ; print fig
def newprint(x, y, zebra): print zebra print x print y
but the function wont do anything to variables outside the function, except the return command changing the value of newprint (once per use.)
two exceptions to scope worth noting: you can change the scope of a variable in python using the global command, and [“a”, “list”] that already exists outside the function can be changed if it is passed as a parameter…
phew. you can see why i avoided functions for so long. but if you have no use for them yet, consider this a preview; and trust that they will be very useful and not seem so complicated when youre more used to coding.
fig covers what i consider to be the 7 fundamentals of high-level coding:
- basic math
you can see why i wait to cover functions. but theyre simpler and more useful than they first appear to be. if you wait a year to learn them, okay. but dont wait a decade, like i did.
one of the reasons i waited so long was that i was able to work around using functions in the silliest ways using basic. in fig, its (thankfully) not at all trivial to reimplement those tricks that i used in basic. and python doesnt have them, so you will need functions a lot sooner than i did.
dont worry: if they dont already make perfect sense, they probably will by that time. python is the language that finally made them reasonable to me. that was 10 years ago.
- license: creative commons cc0 1.0 (public domain)