Checking whether or not an unpublished comment exists on node for current user
You can achieve this in either of the following way,

1) If you are writing custom function as described in question, 
   a) Write your function in a block by selecting text format as PHP code, and display the   block in desired region.
   b) Otherwise define the function in your module and call it in your node template file node--type.tpl.php

2) Through view: Create a view to show un published comments of a node as follows,

View

Create a view:
Settings:
  Show: Comments
  Create block: select
  Click save &amp; continue

Filter criteria
 Comment: Approved (No) 
Advanced->Add
 Contextual filters


Content: Nid

Under When the filter value is NOT available

Provide default value : Content ID from URL
Comment: Author uid 
  Provide default value ->choose user id from logged in user


Now assign your block to a region on the node display page, you will get unpublished comments of the particular user for that node,