I have a function inside another function that is called by main().

Inside the innermost function a vector is created, which I would like to return to the first function, which I would further like to return to main(). I suppose that I could create the vector in main and use it as a parameter in the other functions. That is one solution. I also think that a pointer or reference would work, although I am unsure how. In addition, if I did create the function in main, wouldn't I want to pass the vector by reference, as opposed to value, for efficiency?

Any help is appreciated.

Recommended Answers

All 4 Replies

>>I have a function inside another function that is called by main().
No you don't because nested functions are illegal in C and C++.

The best way to do that is to pass the vector by reference and let the called function populate it. It would be done by passing back a pointer to the vector, be the most efficient and least error prone is to just pass it by reference as shown below.

void foo(vector<string>& ay)
{

}

int main()
{
    vector<string> myArray;
    foo(myArray);
}

really?... never knew that. Since when can a function not call another function?

Isn't main a function that calls other functions and, consequently, contains nested functions?

Definition of a nested function:

int main()
{
    // nested function here
    void foo()
    {

    }
}

The above is a nested function because function foo() is defined within function main(). That is not legal.

For legal way to do it see the example in my previous post, which is probably what you intended.

I've got all the prototypes defined at the top. I guess what I meant to say is that a function inside main calls another function. I'm going to take a look a this reference thing and see if I can make the program compile. I'm sure I'll be back. Thanks for the help so far.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.