If you’re asking whether you can write a function in C++ instead of JavaScript, unfortunately currently (MongoDB version 4.4) the only supported language is JavaScript.
What kind of operations were you trying to perform using $function ? If it’s possible I would recommend to try using MongoDB Aggregation Pipeline.