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

This topic is 1447 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites

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

Share on other sites

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?

Share on other sites

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

Share on other sites

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

Share on other sites

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

Share on other sites

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

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

Share on other sites

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

Share on other sites

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.