A function is a set of code that carries out a specifc task
We can think of the function as having:
We think of the Engine as doing somthing with the input and then producing an output.
In this example, we imagine a function that will calculate the square of the number given to it.
The maths is simple, but this is for a demonstration so that you understand how it works.
Notice that the function has been given a name and that the input is inside brackets to indicate what it is using for the calculation.
When writing code, we generalise functions so that they can be used for ANY relevant input.
This is based on algebraic principles where we use named operands with operators to create an equation.
The equation is simple: square = number * number
Now it does not matter what number we give to the function because the engine is using names for the operands - just like algebra.
Here is how the code for the function is written in JS.
This is based on algebra where we use named operands with operators to create an equation.
The equation is simple: square = number * number
Here is how the code for MAIN routine and SUBROUTINE are written in JS.
The concept is that there is a controlling MAIN routine which calls a SUBROUTINE
This video shows the order in which the program is executed.