ajax recursive calls on wordpress returning answers outsite the function scope

Plugins and your theme are loaded on every request, including AJAX, so any code in them is parsed. They need to be so that any AJAX hooks are found and executed.

Most of the code isn’t executed though, because it’s in functions that are hooked to hooks that aren’t executed during an AJAX request. Some of it is though. If you’re filtering pre_get_posts() in your theme or a plugin, then you probably want that filter to be run during any queries in an AJAX request. So plugins and themes need to be loaded in their entirety to do this.

But if you’re running code outside a function in your theme’s functions file then that’s going to be executed as soon as the theme is loaded, so it will be output on the front-end and in AJAX requests. But the thing is: you shouldn’t be writing code in functions.php that isn’t inside a function. Just loading functions.php shouldn’t cause any output.