Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Syntax of PL/pgSQL
Function parameters
16. DEFAULT value for parameters
Handling exceptions
Summary

Instruction

Good job! Suppose we would like to have an IN parameter with a default value so we don't have to specify it every time we invoke the function. In this situation, we can use the DEFAULT keyword when declaring a function. The syntax will look like this:

CREATE FUNCTION get_count_of_earners(salary_val IN decimal DEFAULT 80000.00) ...

We can declare functions with default values for some or all IN parameters, and the specified default values will be inserted every time the function is invoked without an argument.

We can omit an optional argument when invoking the function:

SELECT get_count_of_earners();

The query above will count the number of employees who earn 80,000 or more (where 80,000 is the default value).

To avoid ambiguity, arguments can only be omitted from the end of the actual argument list. Thus, all parameters after a parameter with a default value must also have default values.

Exercise

Amend the get_count_of_earners(salary_val IN decimal) function so the IN parameter salary_val has a default value of 80000.00.

Look at the query after the function definition: the last call of the function has no arguments passed in, so it should return the same result as the second function call with an explicit parameter of 80000.00.