How to use custom function in posts and pages templates

One problem (there may be more 😉 is that $post->ID is probably out of scope when you call it from the page template.

You could change the way the function works to take the post ID as a parameter, like this:

function my_related_artickes($postID)
{
    $categories = wp_get_post_categories( $postID ); 
    $ids = array();
    foreach( $categories as $cat ){
        $ids[] = $cat;
    }
    return $ids;
}

Then call it like this:

$ids = my_related_articles($post->ID);

Obviously when you call it make sure that you give it a good post ID.