Can a function be a "friend" of another function?

In my application, I have a function foo() that has some local vector objects. I need to call a function bar() from this function numerous times. The function bar() needs to access those vector objects.

I know I can pass by reference or I can declare those variables as global.

Is there any other way in which I can implement this so that my program does not get polluted with global variables?

This will only make sense of course if the vectors have something in common or are part of an entity. Again, it's hard to judge without knowing more specifics about your program. In general I would advise against global variables or befriending things unless there is a very specific reason. Although they don't necessarily mean bad design, they are often a symptom of it if used improperly.

However, putting the two functions together in a class, just to avoid having to pass the vectors as parameters, only makes sense if the class actually represents some sensible entity with a meaningful state.

@MikeyBoy
I was worried about the time complexity. If I pass by reference then would the parameters be passed without any overhead? I need to call the function bar() multiple times from foo() and time is going to be a major constraint in my program.

A lambda function is not really meant to circumvent this kind of thing.

The idea behind passing by reference is that you are really only passing just that, a reference into the function. This means the whole vector does not need to be copied, but the function can alter it in its original place in memory.

I only skimmed that article, but it looks as though it only applied to specific circumstances where rvalues are involved. It may or may not be useful for the OP's situation, since s/he hasn't shown us the specifics of their code.

I believe the RV talked about in the article is return values (hence return value optimization), rather than rvalues. Please correct me if that's what you meant, it's easy to get confused in these things.

From what I gather from that article the author states that compilers use return value optimization is such a way that passing or returning by value is actually not expensive at all.

Again, I hope some of the experts here can chime in and shed their light on it.