I am trying to get these to display in a prompt window, but it only displays the main() function and then exits the prompt window. Can anyone help me understand what I'm doing wrong?
I kinda get the feeling you didn't quite understand what exactly functions are. You can think of them as some sort of program on their own, with their own variables, their own logic etc. A function isn't executed automatically, it only runs when it is called.
It works like this:
1 2 3 4 5 6 7 8 9 10 11 12
void do_stuff_function();
int main()
{
do_stuff_function(); /* here, do_stuff_function is "called"
- this means, control is handed over to the do_stuff_function,
which will then do whatever it does, and then return control to the caller.
*/
return 0;
}
The same goes for the [code]main
function, but it is special in one way: it is the entry point of the program and is automatically called by the system.
I am not sure if I explained this well, so bear with me if it's hard to understand.
I told you the logic behind it. Then you said you got that part, you just didn't get the code part.
Then I showed you the code part. Now you're telling me you don't get the logic?
@lord didn't u said that u got the logic? is it that hard to call function within a function?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void ShowMeNumber();
int main()
{
cout << "The number is:";
ShowMeNumber(); //this will call function called "ShowMeNumber()" and it will do everything coded in function
cin.get();
return 0;
}
void ShowMeNumber() // this is function, everything in it will happen when fucntion will be called
{
cout << "3";
}
The number is: 3
// output, this will be displayed in console
wow... that was hard
how do u wanna make program to do something if u won't tell him what to do?
if u won't understand this, please, leave programming, and do something easier, like cooking or something
edit: +, why are u still initializing variables? like
1 2 3 4 5 6 7 8 9 10
int num1 = 64;
int num2 = 32;
int result = num1 + num2;
cout << "64 + 32 = " << result << endl;
}
{
int num1 = 256;
int num2 = 128;
int result = num1 - num2;
cout << "256 - 128 = " << result << endl;
it should be:
1 2 3 4 5 6 7 8 9 10
int num1 = 64;
int num2 = 32;
int result = num1 + num2;
cout << "64 + 32 = " << result << endl;
}
{
num1 = 256;
num2 = 128;
result = num1 - num2;
cout << "256 - 128 = " << result << endl;