Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Side-effects in functions
6. Functions returning void


Great! You already know the tables and all the attributes we have for about discussion topics, registered users, and their posts.

Remember: In PostgreSQL, it's possible to write a function that does not return anything.

If a function is not supposed to return a value, you should specify void as its return type. Such functions usually modify the database: they insert or delete rows in table, or they update data stored in tables.

The example below shows how to create a new function that inserts a new event into the event table:

CREATE FUNCTION add_event(id smallint, name varchar(60), city varchar(60))
RETURNS void AS $$
    (id, name, city, created_date)
    (id, name, city, current_date);
$$ LANGUAGE plpgsql;

The function inserts a row into the event table based on the values passed as arguments and the current date.


Create a new function named add_topic() that takes only one argument: topic_name of type varchar(255).

Insert a new row into the topic table. The name should be topic_name, and the created_date should be a current date. Don't worry about id – we defined it as serial – the database will assign a correct value by itself.