Functions and Commands
Last updated
Was this helpful?
Last updated
Was this helpful?
You know what a function is and why they are useful.
You can define a function with arguments and return a value.
You know that a command is nothing else than a call to a function.
In some cases, we want to define functions that we can reuse multiple times in our program. Why would we want to do that? There can be multiple reasons:
We want to keep our code as lean as possible. Functions allow us to remove redundant code.
We want to keep our code maintainable. If we make changes to a specific part of our code, we want to avoid making these changes in many places. A change to a function affects all occurrences where the function is used.
We want to structure our code. Functions are the first step to introducing a structure to our code. Most code editors can collapse functions to improve the code's readability.
Consider the function definition from our program below:
The keyword function
tells our program that we want to define a function. The keyword is followed by a name that we can choose. This is very similar to the declaration of a variable. Following the name, we can specify one or more arguments for our function within the opening and closing brackets. In the example above, we do not specify any arguments. After the list of arguments we start the code block that makes up the function with an opening curly brace. To add a visual aid, we slightly indent the code that belongs to a block. This makes it easier to read and comprehend. Finally, we indicate the end of our function with the closing curly brace.
Once defined, we can type the name of the function and append opening and closing brackets to execute it. We could call the function from above like this:
Our program looks up the code execute for that particular functions and runs it. In this case, it is only one line to print the instructions to the console. But the function could be much more complex and contain a complex program made up of many variable declarations, commands, loops, control structures, and even nested functions.
Read more about JavaScript functions under this link:
We learned that the writeLog()
command is actually a function. A function in turn is a block of code with a name. Optionally, a function can have one or more arguments that we can pass with every execution of the function. In most cases, a function returns a result when done executing. That said, there are functions that don't return a result but rather only execute a sequence of steps.
Let's look at the following line of our program from above:
When you look at the code in action, you notice that this line produces an output on the console (the text area on the website) with the exact content that is specified within the brackets and double quotes. We could substitute the text with anything else, and as a result, we'd see the new content being printed to the console. The writeLog()
command allows us to print any arbitrary text to the console, and we can specify the text by passing it as an argument of the function. The general form of the command looks like this:
writeLog(text)
The argument text
must be replaced with a specific value when using the command. As you can see by looking at the whole program above, we are using the writeLog()
command in many places in our code. In all the cases, the value we print is different. However, the command is the same.
The writeLog()
command is provided for us as a function from an external , and we don't need to know how the function behind it looks like (although we could look it up). We are only interested in what the command does for us: print something to the console.
Behind the scenes, the writeLog()
command is a JavaScript : a block of code that does something for us, optionally based on an input that we can specify as an argument. We assign this code block a name (here: writeLog
), and we can then call it when we need it, and it runs the code block.