C++ Functions run when I don't want them to

Started by
15 comments, last by DejaimeNeto 10 years, 2 months ago

I want to make a function in main that will not run however just making the function causes it to run

I tried prototyping the function but I am using object that are made and in main and that is out of scope
That goes the same for above main, as the variables/objects inside were defined in main
How do I stop the program from running the function without calling it when I create it?

Advertisement

Could you post your code?

I have never seen a function running without being called. The code would help us understand.

Additionally, try putting a breakpoint in the function and look through the call stack to see why the function is called.

I really doubt it's being run without being called, but it might be a more obscure call that you don't see right away.

Hello to all my stalkers.

My program has SFML so I am not going to post that code but here is simple version of what I mean


#include <iostream>

using namespace std;

int main()
{
    void function();
    {
        cout << "Hello world!" << endl;
    }

    return 0;
}

The program will say Hello World

but I want it to only run when i call the function later on

How do I stop that?

You can't create local functions in C++. (Although lambdas are similar, but that's not what you are trying to do so forget about that)

void function();

just declares the function prototype (the semicolon at the end gives it away).

I think you are confused...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

My program has SFML so I am not going to post that code but here is simple version of what I mean


#include <iostream>

using namespace std;

void function()
{
    cout << "Hello world!" << endl;
}

int main()
{
    return 0;
}

The program will say Hello World

but I want it to only run when i call the function later on

How do I stop that?

That's how you prevent it anyway.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Alternatively:


#include <iostream>
 
void myFunction(); //Declaration, allows main function to call this if it wants to, even though definition isn't here.
 
using namespace std;
 
int main()
{
 
    return 0;
}
 
void myFunction() //Definition
{
    cout << "Hello world!" << endl;
}

Hello to all my stalkers.

My program has SFML so I am not going to post that code but here is simple version of what I mean

#include <iostream>

using namespace std;

int main()
{
    void function();
    {
        cout << "Hello world!" << endl;
    }

    return 0;
}

The program will say Hello Worldbut I want it to only run when i call the function later onHow do I stop that?

That's not a function. That's a forward declaration of a function followed by some random code surrounded by a scope block

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

My program has SFML so I am not going to post that code but here is simple version of what I mean


#include <iostream>

using namespace std;

int main()
{
    void function();
    {
        cout << "Hello world!" << endl;
    }

    return 0;
}

The program will say Hello World
but I want it to only run when i call the function later on
How do I stop that?

I see the problem, as previously noted by Paradigm Shifter and Washu, you're not coding a "function inside main".
What you are doing is declaring a function:


void function(); // Notice that ";", it marks the end of the function declaration



and then adding "Hello World!" to cout.


{
    cout << "Hello World!" << endl;
}

the { and } are just block separators, that you can use to limit scopes, between some other things.
So the function and the { cout } aren't related by any means.

This topic is closed to new replies.

Advertisement