only use extended join-clause if some variable is TRUE
In case its a (multi)-polygon, I want to extend the join-clause with additional join-clauses:

What you ask for can be achieved with plain boolean logic in the JOIN clause. Append:
AND ((st_geometrytype(PAR_geom) IN ('ST_MultiPolygon', 'ST_Polygon')) IS NOT TRUE

  OR a.path[VAR_main_id] = 1
 AND a.path[array_upper(a.path, 1)] &gt; 1
 AND b.path[array_upper(b.path, 1)] &gt; 1

  OR a.path[VAR_main_id] &gt; 1
 AND b.path[array_upper(b.path, 1)] &lt;= 4 
)

Meaning, either the type is not among the listed ones ('ST_MultiPolygon', 'ST_Polygon'), or the following conditions have to be met.
Operator precedence makes AND bind before OR, so we need no additional parentheses.
The function might look like this:
CREATE OR REPLACE FUNCTION test_return(par_geom      geometry
                                     , par_tolerance float8 = 0.0000001)
  RETURNS SETOF geometry
  LANGUAGE plpgsql AS
$func$
DECLARE
   var_main_id int;
BEGIN
   CASE ST_GeometryType(par_geom)
   WHEN 'ST_LineString'  THEN
       var_main_id = 0;
   WHEN 'ST_Polygon', 'ST_MultiLineString', 'ST_MultiPoint'  THEN
       var_main_id = 1;
   WHEN 'ST_MultiPolygon' THEN 
       var_main_id = 2;
   ELSE 
       RAISE EXCEPTION 'this function does not work on single-points';
   END CASE;

   RETURN QUERY
   WITH repeated_pts AS (
      SELECT d.path, d.geom 
      FROM   st_dumppoints(par_geom) d  -- evaluate once
      WHERE  st_npoints(st_snaptogrid(par_geom, par_tolerance)) &lt;&gt; st_npoints(par_geom)
   )   
   SELECT b.geom
   FROM   repeated_pts a
   JOIN   repeated_pts b ON a.path[1:var_main_id] = b.path[1:var_main_id] 
                        AND a.path[array_upper(a.path, 1)] &lt; b.path[array_upper(b.path, 1)] 
                        AND st_dwithin(a.geom, b.geom, par_tolerance)
                        -- append here!
                        AND ((st_geometrytype(PAR_geom) IN ('ST_MultiPolygon', 'ST_Polygon')) IS NOT TRUE

                          OR a.path[VAR_main_id] = 1
                         AND a.path[array_upper(a.path, 1)] &gt; 1
                         AND b.path[array_upper(b.path, 1)] &gt; 1

                          OR a.path[VAR_main_id] &gt; 1
                         AND b.path[array_upper(b.path, 1)] &lt;= 4 
                        );
END
$func$

Might be possible to simplify further. I lack insight.