Scripting in LUA: PART 2 [Functions, Parameters, Scope, Returning & Conditionals]

Cheryl Abram
6 min readMay 25, 2022

Functions

A function is a block of code that does something. For example, if I want to print or output “Hello” a bunch of times, I can write it a bunch of times or I can create a function to define the code one time then run it.

Function means action!!!

Functions (sometimes called procedures) are mini programs that I can use over and over inside of your bigger program.

It’s like the chorus in a song. In music, a chorus is a repeated section that contains the primary musical and lyrical motifs of the song.

Remember those old cassette tapes and the paper inside them that contained the lyrics? When writing the lyrics they would sometimes write the chorus once, then everytime the chorus was sung afterward, they would write “chorus” instead of all the words in the chorus.

It’s mainly the chorus of the song what we remember the most.

It’s actually kinda like a variable in that “chorus” means all the words that you wrote previously.

It’s the function that performs a task.

This is a great unplugged activity to do and understand functions.

The parentheses after the function name “BestFunction” () is the function closure and make it possible for various scripts not to interfere with each other even though they define similarly named variables or private functions.

Those privates are visible and accessible only within closure itself and not outside of it.

Here, the “chorus” or function or task to be performed is “BestFunction()”.

To “end” means it’s the end of the code.

This defines the function but does not run it.

The “print” does not give me an output here b/c the function has not yet been called.

To call a function means to run it and to run it I have to type the function

Calling the function runs everything before “end”.

Here is another example. Before we leave functions, I’m going to have my boys do the Cash App activity to understand the importance of functions and using them where a variety of things can happen (like amount of money).

Now, I’m wondering where functions can be hacked and what can I do to manage that risk. One resource that I found for this is here, but I’ll get more to that later. For now, I’m taking note that assert() is something that I need to look into.

I’m also going to revisit this Dangerous Functions in Python Link

SCOPE & RETURNING

Scope

Local (or function) scope is the code block or body of any function or. These names will only be visible from the code of the function.

“Local” has something to do with scope here.

local with no indents means the entire script can access it.

Placing local in the code under a function restricts the variable to that function.

The second local function does not know what “a” is equal to b/c it is only scoped to the first function.

A function can be equal to a variable. This function is running the code and returning “Awesome”.

Returning makes little sense to me right now but I’m sure I’ll get it as I go further into actually writing scripts. However, I did find a fairly clear definition here

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function. For more information, see Return type.

PARAMETERS

Also called arguments. Something you give when you call the function to make is do something slightly different than it did before. Think of the song Old MacDonald. Each animal changed the chorus a little bit.

A parameter is an extra piece of information you can give a function to customize it for a special need. Think of making an ice cream sunday but with different “toppings”. Think of each topping as a parameter.

“add” is what I want the function to do.

Parameters go inside the parentheses. Be sure to put a comma between the parameters.

The output is 5.

What will this do? I called the function 3 different times and set the value of hotdog to three different values each time.

CONDITIONALS (IF, AND, OR, THEN)

If statements are akin to if-then statements in logic modeling.

“if I have money, then I can go to the store. If I go to the store, then I can buy something”….

As an activity, I’m going to have my boys complete these 7 conditionals to help them understand:

IF I pick up a book,
THEN get up and write the letter S on the whiteboard and sit back down.

IF someone writes the letter S on the whiteboard,
THEN go open and close the front door and sit back down.

IF someone opens and closes the front door,
THEN get up and write the letter T (after the letter S) on the whiteboard.

IF someone writes the letter T on the whiteboard,
THEN get up and turn the living room lights on and off and sit back down.

IF someone turns the living room lights on and off ,
THEN get up and write the letter O (after the letter T) on the whiteboard.

IF someone writes the letter O on the whiteboard,
THEN get up and take a pillow off the couch.

IF someone takes a pillow off the couch,
THEN get up and write the letter P (after the letter O) on the whiteboard.

This says, if 3 equals 3 then print or output “yes”.

I can also use “and” in the statement

I can also use “or” in the statement

I can also use BOOLEAN statements

Here’s another one using the Baseplate variable. The first time I ran this it did not work because I misspelled “baseplate”

This is cool too and I can better INTERPRET what I’m actually telling the code to do.

--

--