What are the benefits of using functions? Let's list the main advantages:
- Functions extend the functionality of a database (we are allowed to add custom functions).
- A function allows you to encapsulate logic that can be invoked like a programming subroutine.
- Code reusability. Imagine that we have to repeat some calculations in our SQL queries. It would be tedious to manually hard-code these calculations. Instead, we can simply create a generic function that takes some inputs and performs the calculations for us.
Warning (portability): each database engine (for instance Oracle, SQL Server, etc.) has slightly different syntax for creating functions, though the concepts are similar in all database engines.