User-defined functions in SQL let you create reusable code blocks that take inputs and return outputs. First, you define the function with a name, parameters, and a body containing SQL logic. This definition is saved in the database. Then, you call the function in a query with specific arguments. The function executes its code using these inputs and returns a result. For example, a function add_numbers(a, b) returns the sum of a and b. When called with 3 and 5, it returns 8. Variables inside the function hold input values and the return value. Defining the function first is essential so SQL knows what to run. Changing inputs changes the output accordingly. The return value is what the query receives from the function call.