Functions are the building blocks of all software. Instead of writing the same code 100 times, you write a function once and call it 100 times. This makes your code readable, maintainable, and much less prone to errors.
A function is like a factory machine. You give it raw materials (inputs/arguments), it does some work, and hands you back a finished product (output/return value).
Building the machine (writing the code inside the function).
The inputs you pass to the machine when you use it.
The final result the machine gives back to you.
A common pitfall is forgetting the 'return' keyword. If a function does calculations but doesn't 'return' the result, the output is undefined. Another mistake is confusing the parameters (variables in the definition) with arguments (the actual values passed in).
Select a machine, provide inputs, and see what it returns!
Test your function knowledge!
The mathematical concept of functions dates back to the 17th century with calculus. In computer science, 'subroutines' (early functions) were invented by David Wheeler in 1952. Today, programming paradigms like Functional Programming rely entirely on the purity and predictability of mathematical functions.