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.

The code you posted is a good example of a pass "by value". What you are asking about is called a pass "by reference". There are 2 ways to accomplish this. You can do it with a pointer or you can do it with a reference.

Pass by reference using pointers:

#include <iostream>

using std::cout;
using std::endl;

void someFunc(int *);

int main() {
  int myInt = 10;     //declare an integer variable
  cout << "Before function:\n\tmyInt = " << myInt << endl;
  someFunc(&myInt);   //call someFunc(), provide reference argument
  cout << "After function:\n\tmyInt = " << myInt << endl;
  return 0;
}

void someFunc(int *pMyInt) {
  cout << "In function, before change:\n\tmyInt = " << *pMyInt << endl;
  (*pMyInt) += 10;    //dereference the pointer, add 10
  cout << "In function, after change:\n\tmyInt = " << *pMyInt << endl;
}

Pass by reference using reference parameter:

#include <iostream>

using std::cout;
using std::endl;

void someFunc(int &);

int main() {
  int myInt = 10;     //declare an integer variable
  cout << "Before function:\n\tmyInt = " << myInt << endl;
  someFunc(myInt);    //call someFunc()
  cout << "After function:\n\tmyInt = " << myInt << endl;
  return 0;
}

void someFunc(int &rMyInt) {
  cout << "In function, before change:\n\tmyInt = " << rMyInt << endl;
  rMyInt += 10;       //add 10 to the parameter
  cout << "In function, after change:\n\tmyInt = " << rMyInt << endl;
}

Edited 6 Years Ago by Fbody: n/a

Thanks... I had a feeling that's how you did it, but I didn't know the sytax

This question has already been answered. Start a new discussion instead.