Complete the code to declare a function that accepts a variadic integer array parameter.
CREATE FUNCTION sum_numbers([1] integer[]) RETURNS integer AS $$ BEGIN RETURN 0; END; $$ LANGUAGE plpgsql;
In PostgreSQL, to declare a variadic parameter, you write VARIADIC before the parameter name and type, like VARIADIC numbers integer[].
Complete the function call to pass multiple integers as a variadic argument.
SELECT sum_numbers([1]);When calling a function with a variadic parameter, you use VARIADIC ARRAY[...] to pass multiple values as one array.
Fix the error in the function definition to correctly use a variadic text parameter.
CREATE FUNCTION concat_texts([1] text) RETURNS text AS $$ BEGIN RETURN ''; END; $$ LANGUAGE plpgsql;
The correct syntax is VARIADIC texts text to declare a variadic parameter of type text.
Fill both blanks to create a function that sums variadic integers and returns the total.
CREATE FUNCTION sum_all([1] integer) RETURNS integer AS $$ DECLARE total integer := 0; BEGIN FOREACH num IN ARRAY [2] LOOP total := total + num; END LOOP; RETURN total; END; $$ LANGUAGE plpgsql;
The function parameter must be declared as VARIADIC nums integer (here VARIADIC nums is the blank), and inside the function, you loop over nums which is the array of integers.
Fill all three blanks to define and call a variadic function that concatenates texts with a separator.
CREATE FUNCTION concat_with_sep([1] text, [2] text) RETURNS text AS $$ BEGIN RETURN array_to_string([3], sep); END; $$ LANGUAGE plpgsql; SELECT concat_with_sep(',', VARIADIC ARRAY['a', 'b', 'c']);
The first parameter is the separator sep, the second is the variadic parameter VARIADIC texts, and inside the function, you use texts as the array to join.