Functions are blocks of code that execute a set of pre-defined tasks that can be ran at any point in code execution.
A function can be declared by using the event statement followed by a name and its arguments. Similar to variables, arguments can store any value, including other functions.
Functions can then be called from anywhere as long as it is in same scope by writing the name and required arguments of the function. Calling a function will execute any code stored inside of it.
As mentioned in Variables, functions are like any other type, meaning you can assign them to a variable. You can do so by setting the variable to the function's name, omitting its arguments.
After assigning a function to a variable, it can be called by using the variable's name.
An optional argument is a function argument with a default value that gets used in the case a value isn't specified in the function call. You can declare an optional argument by using the = sign after the argument name. Optional arguments can only be declared after the required arguments.
Optional arguments can also be defined by using the [] syntax.
If we call this function without specifying the optional argument, it will default to counting in steps of 1 but we can also specify the step count to any arbitrary number we want.
Functions can optionally return any value at any point in its execution. You can specify a return value by using the return statement.
return statements will completely stop the execution of a function, which means that any code after the return statement won't get ran.