Why are functions important?
Functions are how programs do work. Think of them as the action that is to be performed by code. Functions can return variables, perform calculations, or draw images on the screen. They are essential to creating applications in that they can define something to be done and to them over and over.
In fact, this is the proper use of a function. Imagine that you need to convert ounces to cups or inches to feet over and over. You could create an application that performs this conversion for you and use the program whenever you need to do the conversion.
How are functions created?
To get started (most languages) use the function key word. A keyword is a word that is pre-defined in a coding language that represents an action. In this case, the function keyword tells the chosen programming language that we are about to create a subroutine of code (an action that will be stored in memory as a variable)
Let’s take a look at a function and break it down.
function nameOfFunction() {
let result = 1000
return result
}
As we talked about earlier, we start with the function keyword. This keyword tells the language that the following code is a block of code that does something.
After the function keyword, we have the name of the function. I have chosen to call it nameOfFunction just to help describe what it does, but it would be any combination of characters we choose. For example, we could give the function a name of asdf123 instead. The program will not care what you name as long as it is within your chosen language’s guidelines for function names.
The next part we see that we should note are the parenthesis characters.
()
These parenthesis will not do anything for our basic example. However, calling attention to them is important as we will use them in a more advanced form of functions later. So take note of them, but don’t worry that they don’t seem to do anything right now.
The next part of our function to take note of are the curly brackets.
{}
These are very important. They define what is called a code block or block of code. Code blocks signify the start and end of a named space. You can think of this area as a separate universe or country. Within this code block whatever we put in it exists between those curly brackets ONLY. So in this example, as we keep moving forward with our function, you can see that we are using the let keyword to make a variable named result and assigning it a value of 1000.
Since result is inside of our curly brace code block it does not exit before or after the curly braces. This helps us make sure that we don’t accidentally assign it a different value in another function and ruin the nameOfFunction routine that we just wrote.
Side note: Don’t worry if this doesn’t make total sense right now. My intention is to start to introduce concepts that we can build on later. Not to introduce a concept like name spaces and have you understand everything about them right away.
The last part of the function that to cover is the line that reads
return result
The return word is also a keyword. This keyword allows messages to be sent outside of the universe we created between our curly brackets. This line tells the program to take the result variable and send it back to whoever (or whatever line of code) called it.
To use our new function, we would do something like the following code.
nameOfFunction()
This tells our program to look for something that we saved named nameOfFunction and do whatever that thing does.
So in our case:
- Set a variable named result and give it a value of 1000
- (on the next line) return the value of result
So in with our function, running it would return 1000. Alternatively we could have written the function as:
nameOfFunction() {
return 100
}
This would have returned the exact same result as our original function, but would not have let us talk about as many concepts. So for teaching and example purposes being a little less efficient helps us learn more.
Next we will talk about more complex functions an the usage of the parenthesis as mentioned earlier.