Q:

doing an ajax request always outputs 0

I'm trying to do an ajax request from my themes header.php file.  I've read that I have to use the admin-ajax.php file to achieve this.  So I looked for examples and found this one. 
My code in the header.php file looks like this:
<script>
$(document).ready(function(){
    $('.test a').click(function(){
        var className = $(this).attr('class');

        $.ajax({
            url: "<? php echo admin_url('admin-ajax.php'); ? >",
            data: {
                'action': 'myFunc',
                'id': className
            },
            success: function(res){
                alert(res);
            },
            error: function(err){
                alert("Error: " + err);
            }
        });
    });
});
</script>

And my PHP code looks like this:
function myFunc() {
    echo "hello";
    die();
}

add_action('wp_ajax_nopriv_myFunc', 'myFunc');

I'm not sure where I should put this PHP code.  Should I put this in my functions.php file or directly to the admin-ajax.php file?  It doesn't matter where I put the PHP code, the output is always 0 (the last line in admin-ajax.php is die('0')).  Or is it because of my url parameter? 

A:

Your custom functions should always go in your theme's functions.php file (if they are not within plugin territory).  You should never try to modify any core files outside the wp-content folder (except wp-config.php) as they will be overridden by future updates. 
Now, there are 2 ways to do what you want. 
1 - Admin-ajax
This simple function will serve you nice to get a hello!  as an output:
add_action('wp_ajax_my_ajax_handler', 'my_ajax_handler');
add_action('wp_ajax_nopriv_my_ajax_handler', 'my_ajax_handler');
function my_ajax_handler (){
    _e('Hello!','text-domain');
    exit();
}

Now, try and access the admin-ajax.php directly to see if you have an answer, by visiting wp-admin/admin-ajax.php?action=my_ajax_handler.  This should work for both logged-in and non-logged-in users. 
2 - REST-API
Even simpler.  Take a look into this:
add_action( 'rest_api_init', function () {
    //Path to ajax function
    register_rest_route( 'Roman/v1', '/say_hello/', array(
            'methods' => 'GET', 
            'callback' => 'my_hello_function' 
    ) );
});

And a function to say hello:
function my_hello_function(){
    return __('Hello!','text-domain');
}

Now, if you send a request to http://example.com/wp-json/Roman/v1/say_hello/, you will get a nice welcome as JSON, like:
{"Hello!"} 
Which you can use in your jQuery script.