Fluttermind
  • news
  • games
  • music
  • contact
  • press kit

Coding for Beginners #3: Functions

4/5/2022

0 Comments

 

Functions

A function is just a block of code with a name. Some will be created by you. Others are part of the language. In Lua, the language we're learning, there are built-in functions allowing coders to perform simple operations without writing having to write their own. print() is one of these. As you've already noticed, it output whatever text or variables you put in between those two brackets.

If you executed the code:
print("Raspberry")

The output will be:
Raspberry
​However, if you'd typed:
print(Raspberry)

...the result would be 'nil', which just means 'no data'.

Why is this? Well, anything you don't put in quotes is assumed to be a piece of data, an ingredient label. We've not created data with the name Raspberry so the value of Raspberry is currently 'nil'. Nada. Zip. Zilch.


We could fix this by adding a line above that print() function to give us:
Raspberry = "This is a bit of data labelled 'Raspberry'"
print(Raspberry)

...and I'm pretty sure you can guess what the output would be!
​See, you're starting to read code, too!

More Built-in Functions

Let's define what functions are a little better:

    Functions are labelled pieces of code which can take data, output data... or have other effects.

There are quite a few functions which are part of Lua. You've seen print() so let's look  at another one: math.floor()

(Don't worry about the '.' between math and floor(). It just means that floor() is part of a group of functions which are all related to maths. There's math.random(), math.ceiling() and a host of others)

The function math.floor() expects you to put a number (or a variable name) between the brackets. The function takes that number and spits it back out, but will have rounded it down to the nearest whole number.
a = 2.5
b = math.floor(a)
print(b)

This outputs 2.5 rounded down which is 2.​

You'll notice that math.floor() is different from print() because we both pass something to it and get something back: in this case a value. If you look at the code above, we pushed the value we got from the function into 'b' using '=', saving it for later use.

We could have written the whole thing like this:
print(math.floor(2.5))

...and got the same result without using variables at all. In this case math.floor() would have just sent the value directly to print(). It's perfectly valid! There are often many ways to do the same thing. Welcome to coding.

Conclusion

As time goes on you'll be creating your own functions for things you want to do over and over again with different information, like adding numbers together, checking sentences for specific words, or displaying your art in a certain way.

It's a handy way to organize your code, and to stop you typing the same stuff over and over again, with the risk of making mistakes each time you do it.

For now, all you need to know is that to use a function, you need to type its name, a pair of brackets, and probably one or more variable names between those brackets. If the function 'returns' something, then you'll often want to pass that into a new variable using '='. It'll usually look something like this:
a = "Some data or other"
b = "Yet more data"
c = whateverFunctionNameWeAreUsing(a, b)
print(c)

As ever, I hope this was useful. If you want to try it out yourself, you can use this web-site and type or copy the snippets of code there.
0 Comments



Leave a Reply.

    Author

    Fluttermind’s director, Dene Carter, is a games industry veteran of over 25 years, and co-founder of Big Blue Box Studios, creators of the Fable franchise for the XBox and XBox 360.

    Dene takes great pride in doing every aspect of game-creation entirely by himself. Nobody knows why.

    Archives

    April 2022
    March 2019
    April 2018
    January 2018
    February 2017
    September 2016
    July 2016
    February 2016
    October 2015
    June 2015
    February 2015
    January 2015
    November 2014
    April 2014
    March 2014
    February 2014
    January 2014
    November 2013
    August 2013
    May 2013
    February 2013
    December 2012
    November 2012

    Categories

    All

Powered by Create your own unique website with customizable templates.
  • news
  • games
  • music
  • contact
  • press kit