Bird
0
0

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:
  1. Step 1: Recall VARIADIC position rule

    VARIADIC parameter must be the last parameter in the function declaration.
  2. 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.
  3. Final Answer:

    CREATE FUNCTION example(fixed int, VARIADIC texts text[]) RETURNS text AS $$ ... $$ LANGUAGE sql; -> Option D
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes