Tap Forms JavaScript Scripting 101
by T. L. Ford

Section 3: Functions

I'm going to cover functions next, simply because that's what you are going to see when you create a new script in Tap Forms.

The English word "function" has a definition. Forget that. It doesn't apply.

In programming, a function is a concept for "a block of code that you can reuse". (Yeah, yeah, programmers. I know about inline functions, anonymous functions, object methods, and how the name is a pointer to some memory. They don't need to know about that right now. What are you still doing here anyway? Shoo!)

A function has two main parts: the function definition and the function call. The definition is just a recipe. The computer ignores it until you try to use it with the function call.

The syntax of the function definition is:

function function_name() { return; }

When the computer sees this, it reads it like:

function - There's one of my keywords! You are going to tell me a recipe. Woohoo!

function_name - a word with no spaces that follows is what you want to call this recipe. Ok, I'll add this to my language dictionary.

( - the start of what you want me to add to my language.

) - no new variables to know about. Great! I'll make a note that you aren't going to give me anything when you call this function. (Much more about what can go inside the parentheses later, under "function parameters".)

{ - the start of your recipe. I'll consider everything up to the matching } as your recipe.

return - All functions return something (give/send back a value) even if that something is undefined or null. Forget the English word definitions for undefined and null - these are specific values that indicate that there isn't anything. return; simply returns undefined or null. If the code had said return 1; , the function would have returned the number 1. If the code had said return "no way man"; , the function would have returned the string no way man .

} - there it is! The end of your recipe definition. Onward!

Don't worry if this is confusing. More examples are coming. You'll get it.

The syntax of the function call is:

function_name()

The computer reads it like this:

function_name - One word, no spaces. Do I have this word in my language? Why yes, you've told me about it in your function definition. Excellent.

( - the beginning of some stuff you are going to give me to use when I do (run) the function (more on that later). Let me look at my definition. You told me you weren't going to give me anything, so the next thing I see better be a ).

) - excellent! I see you are a good programmer. I wasn't expecting anything and you didn't give me anything. I guess I have to go run that function now. Where's that definition...

Notice there isn't semi-colon anywhere in the function syntax. A ; is the end of a command/instruction. A function definition is just the recipe. A function call can be a command/instruction or used as a variable. More on that later.

Let's look at the actual default Tap Forms script and how the computer interprets it:

function - There's my function keyword. You are defining a function (a recipe of commands that I can do later).

Script - There's your function name. Ok. I'll add it to my language. I'm sure you know that things I add to my language are case sensitive. Capitalization matters. Script is not the same as script is not the same as sCrIpt is not the same as scripT is not the same as scrIpt is not the same as sCript.

( - You are going to tell me what you are going to give me to use when I run this function.

) - You aren't going to give me anything. Great. I'll make a note of that.

{ - The beginning of your commands. Yeah yeah. I'll just write this in my book. You aren't telling me to do these commands right now. I can skip to the matching }.

} - There it is. The end of your recipe. I'll put those commands in my book for later.

Nothing to see here, move along. This is just white space (spaces, tabs, line endings - I use these to identify the start and ends of keywords and symbols).

Script - Let's see, do I have this word in my language? Yes, you told me about it earlier. According to my notes, it's a function. I expect to see a ( next.

( - Sure enough! I expect to see a ) next because you said you weren't going to give me anything.

) - There it is! You are rocking this code thing. I need to go run this function now because you gave me a function call.

Back to the definition. You didn't give me anything to use with this function (more on that later), so I can just carry on.

// - Woohoo!!! Vacation time!!! This is a comment and I don't have to do anything. I can go to the Caribbean.

var - Nuts. So much for vacationing. That's one of my keywords and var means you are going to give me another word to add to my language dictionary. Sigh. But we're inside a function, so I'm only going to make a note of it here for this function only. I refuse to clutter the rest of my language dictionary. I'm only going to know about it when I'm inside this function code as specified by the { and }. This is called variable scope or scope of a variable or sometimes just referred to as scope. I'll know nothing about the variable outside of these {}'s.

hello_world - There's the name for your variable. Ok. I added it to my language dictionary.

= - You're going to tell me what to put (store) in this variable.

" - Beginning of your string. I'll make a note that this variable contains a string. It could have been a number or a boolean (true/false) or anything really.

Hello World! - Ok. I stored this.

; - end of the command. What should I do next?

return - Phew! That's the keyword that says I can be done running this function and what you are about to give me is what I'm going to send back. If I see a ; next, I'm going to return null/undefined.

hello_world - Is this word in my language dictionary? Sure enough, here it is for this function block. It's a variable. Lemme look ahead a little. You don't have an = after it, so you aren't telling me to store something (assigning a value to the variable). Instead, you are asking me to get you the value. Lemme dig in my notes. You gave me the string Hello World!. Ok, you can have that. I'll return Hello World! now.

Script() is now Hello World!.

; - End of the command.

No more lines of code. Yippee !!!! I'm done !!!! Ah, wait, I'm working inside Tap Forms. I shall announce this as the Result output of this script.

Here's this code in action:

The computer does all that awful fast, doesn't it? But where did I find that script? It's time to talk about the Tap Forms script interface.

NEXT - Section 4: Tap Forms Script Interface