In programming, a function is a reusable block of code.
Think of it like a cardboard box on a production line with a simple name written on the side like “tie knot” - this function accepts a ribbon and produces the same ribbon with a particular kind of knot tied in it. Other functions might not accept any input, but produce an output of a similar format over and over again, or accept an optional input. The most important thing to remember is that functions should be re-usable and best practice follows that functions should only do one thing, making the code easier to maintain and test.
There are two types of functions - ones built into the programming language you are using like .split(" ") in TypeScript which can be applied to a string to split it by its input (in this case a space, making the function split by word), and ones you define yourself. Every function you define must have a unique name.
Think of it like a cardboard box on a production line with a simple name written on the side like “tie knot” - this function accepts a ribbon and produces the same ribbon with a particular kind of knot tied in it. Other functions might not accept any input, but produce an output of a similar format over and over again, or accept an optional input. The most important thing to remember is that functions should be re-usable and best practice follows that functions should only do one thing, making the code easier to maintain and test.
There are two types of functions - ones built into the programming language you are using like .split(" ") in TypeScript which can be applied to a string to split it by its input (in this case a space, making the function split by word), and ones you define yourself. Every function you define must have a unique name.