Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by Cryusaki Feb 2, 2014 at 5:19 PM 15 replies 3.3k views
Original Post
Cryusaki
Cryusaki

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?

DejaimeNeto
DejaimeNeto

Could you post your code?

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

Lactose
Lactose

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.
Cryusaki
Cryusaki

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?

Paradigm Shifter
Paradigm Shifter

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
Paradigm Shifter
Paradigm Shifter

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
Lactose
Lactose

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.
Washu
Washu

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.
DejaimeNeto
DejaimeNeto

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.

Cryusaki
Cryusaki

I see my issue now, I haven't coded in a long time but I can't believe I made such a basic mistake

Thanks everyone but I have a new issue related to my original issue

How do I make a function that can work with a variable declared in the main?


#include <iostream>

using namespace std;

void function();

int main()
{
    int x = 5;

    return 0;
}

void function()
{
        cout << x << endl;
}

I get an error at second last line saying x is out of scope, I understand why I just don't know how to solve this

Washu
Washu

You need to either pass in the variable (good), or use a global (bad).

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.
Lactose
Lactose


#include <iostream>
 
using namespace std;
 
 
void function(int x);
 
int main()
{
    int x = 5;
    function(x);
    function(18); //Different example, using value instead of variable.
 
    return 0;
}
 
void function(int x)
{
    cout << x << endl;
}
Hello to all my stalkers.
BeerNutts
BeerNutts

I see my issue now, I haven't coded in a long time but I can't believe I made such a basic mistake

Thanks everyone but I have a new issue related to my original issue

How do I make a function that can work with a variable declared in the main?


#include <iostream>

using namespace std;

void function();

int main()
{
    int x = 5;

    return 0;
}

void function()
{
        cout << x << endl;
}

I get an error at second last line saying x is out of scope, I understand why I just don't know how to solve this

The answer is find a good beginner's C++ tutorial, and start over. You've got a lot to learn, and it will be easier on you (and us) if you take a little time to understand the basics.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
minibutmany
minibutmany




I try not to use classes in C++ because of how functions are called by themself. C style object orientation is more flexible using structs, virtual function tables and separate methods.

Before it was called C++, it was called "C with Classes".

Stay gold, Pony Boy.
DejaimeNeto
DejaimeNeto

Before it was called C++, it was called "C with Classes".

I used to be called a baby. That was 20+ years ago.
Khatharr
Khatharr

Wow. It was a pretty ignorant comment, but -21 is pretty harsh for the beginner's forum. Let's calm down a little, lol.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
DejaimeNeto
DejaimeNeto

Wow. It was a pretty ignorant comment, but -21 is pretty harsh for the beginner's forum. Let's calm down a little, lol.

Actually, it is not only ignorant, but a quite misleading comment, and targeted at an absolute beginner, what makes matters worse. Given that the up vote hover message is: This post provides useful information and demonstrates knowledge of the subject, voting it up would make absolutely no sense.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.