here's my code:
#include <iostream>
using namespace std;
void func(int num) {
num = num + 1;
cout << num;
}
int main() {
int num2 = 5;
cout << num2;
func(num2);
cout << num2;
func(num2);
cout << endl << endl;
system("PAUSE");
}
when the program runs, it yields an output screen that looks like this,
5656
press any key to continue...
as it should, however I'm wondering if there's a way to pass the variable "num2" on to the function "func()", change the data in "num2", and then return back to the main function with the newly updated "num2" variable. ultimately what I would like the program to do is increase "num2" by 1 every time "func()" is called.
EXAMPLE... what I want my output screen to look like is this,
5667
press any key to continue...
I know that I could just increase the value of "num2" inside of the main function. more or less, this is just a sample code of the full thing that I'm trying to accomplish, so please don't provide this as a solution.
I've alreadey considered making the function return the value of "num" and using the statement "num2 = func(num2);", so please don't provide that as an answer. I'll use that if there's no other way, however it just seemed possible to do it other ways.
correct me if I'm wrong, but I think you can do it with pointers. I don't know how but I sure that's how you would do it. given that passing a variable to a function realy just passes it's data to the function, I figured that you could pass the pointer for a variable to the function and allow the function to manipulate that variable.
thanks for the help... if there's no solution, just tell me and I'll use my other solution. I've been programming for a while, so don't worry about saying anything I won't understand.