\$20

### Image of the Day Submit

IOTD | Top Screenshots

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

16 replies to this topic

### #1Cryusaki  Members

Posted 02 February 2014 - 11:19 AM

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?

### #2dejaime  Members

Posted 02 February 2014 - 11:23 AM

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

### #3Lactose  GDNet+

Posted 02 February 2014 - 11:35 AM

POPULAR

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.

### #4Cryusaki  Members

Posted 02 February 2014 - 12:00 PM

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?

Posted 02 February 2014 - 12:03 PM

POPULAR

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

Posted 02 February 2014 - 12:04 PM

POPULAR

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.

Edited by Paradigm Shifter, 02 February 2014 - 12:05 PM.

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

### #7Lactose  GDNet+

Posted 02 February 2014 - 12:07 PM

POPULAR

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.

### #8Washu  Senior Moderators

Posted 02 February 2014 - 04:23 PM

POPULAR

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.
ScapeCode - Blog | SlimDX

### #9dejaime  Members

Posted 02 February 2014 - 04:31 PM

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.

Edited by dejaime, 02 February 2014 - 06:22 PM.

### #10Cryusaki  Members

Posted 02 February 2014 - 05:39 PM

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

### #11Washu  Senior Moderators

Posted 02 February 2014 - 05:43 PM

POPULAR

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.
ScapeCode - Blog | SlimDX

### #12Lactose  GDNet+

Posted 02 February 2014 - 05:47 PM

POPULAR

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

### #13BeerNutts  Members

Posted 03 February 2014 - 12:42 PM

POPULAR

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)

### #14minibutmany  Members

Posted 03 February 2014 - 04:38 PM

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.

### #15dejaime  Members

Posted 03 February 2014 - 04:44 PM

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

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

### #16Khatharr  Members

Posted 06 February 2014 - 06:46 PM

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.

### #17dejaime  Members

Posted 06 February 2014 - 07:03 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.