A SQL function gives a repeated calculation a named interface. Declare its argument types, return type and behavior when input is missing. The body can use positional arguments or named parameters. A reusable function still needs a clear contract so callers don't confuse a line total with a unit cost.
Volatility describes how stable the result is. IMMUTABLE promises the same result for the same arguments across all calls. STABLE can read a consistent database snapshot within a statement. VOLATILE allows changing results and side effects. Incorrectly declaring a data-dependent function immutable can let the planner reuse an answer that no longer holds.
Use a small SQL function for a straightforward expression. A procedural language is useful when the task needs control flow, but it adds another place to inspect logic and permissions. Test NULLs and empty inputs instead of relying only on a normal row.
A line extension multiplies units by cost and keeps the original line grain.