You want to create a function that accepts a fixed integer and then any number of text arguments variadically. Which of these function declarations is correct?
hard📝 Application Q15 of 15
PostgreSQL - Advanced PL/pgSQL
You want to create a function that accepts a fixed integer and then any number of text arguments variadically. Which of these function declarations is correct?
ACREATE FUNCTION example(fixed int, VARIADIC texts text) RETURNS text AS $$ ... $$ LANGUAGE sql;
BCREATE FUNCTION example(VARIADIC texts text[], fixed int) RETURNS text AS $$ ... $$ LANGUAGE sql;
CCREATE FUNCTION example(fixed int, texts VARIADIC text[]) RETURNS text AS $$ ... $$ LANGUAGE sql;
DCREATE FUNCTION example(fixed int, VARIADIC texts text[]) RETURNS text AS $$ ... $$ LANGUAGE sql;
Step-by-Step Solution
Solution:
Step 1: Recall VARIADIC position rule
VARIADIC parameter must be the last parameter in the function declaration.
Step 2: Check parameter types and order
CREATE FUNCTION example(fixed int, VARIADIC texts text[]) RETURNS text AS $$ ... $$ LANGUAGE sql; places fixed int first and VARIADIC texts text[] last, which is correct. CREATE FUNCTION example(VARIADIC texts text[], fixed int) RETURNS text AS $$ ... $$ LANGUAGE sql; places VARIADIC first, which is invalid. CREATE FUNCTION example(fixed int, texts VARIADIC text[]) RETURNS text AS $$ ... $$ LANGUAGE sql; misplaces VARIADIC keyword. CREATE FUNCTION example(fixed int, VARIADIC texts text) RETURNS text AS $$ ... $$ LANGUAGE sql; uses non-array type with VARIADIC.
Final Answer:
CREATE FUNCTION example(fixed int, VARIADIC texts text[]) RETURNS text AS $$ ... $$ LANGUAGE sql; -> Option D
Quick Check:
VARIADIC last and array type = correct [OK]
Quick Trick:VARIADIC param must be last and array type [OK]
Common Mistakes:
Placing VARIADIC parameter before fixed parameters
Using scalar type with VARIADIC
Misplacing VARIADIC keyword after parameter name
Master "Advanced PL/pgSQL" in PostgreSQL
9 interactive learning modes - each teaches the same concept differently