Passing references/pointers

I'm working with using functions to return more than 1 value (using pointers/references) at the moment. I'm a little confused about the difference (if there even is one) and curious if it really matters. I want to know if one is more effective than the other, if one is necessary for some situations and the other is necessary for other situations, if it's all preference, etc. Here are my two codes (if anyone cares.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Passing pointers to a function
#include <iostream>
using namespace std;

int Factor(int n, int *Squared, int *Cubed);

int main()
{
	int myNum;
	int mySquared;
	int myCubed;

	cout << "Enter a number (0-20): ";
	cin >> myNum;

	int isGood = Factor(myNum, &mySquared, &myCubed);

	if (isGood)
	{
		cout << "Squared: " << mySquared << endl;
		cout << "Cubed: " << myCubed << endl;
	} else {
		cout << "ERROR!" << endl;
	}

	return 0;
}

int Factor(int n, int *Squared, int *Cubed)
{
	int Value = 0;
	if (n > 20 || n < 0)
	{
		Value = 0;
	} else {
		*Squared = n * n;
		*Cubed = n * n * n;
		Value = 1;
	}
	return Value;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Passing references to a function
#include <iostream>
using namespace std;

int Factor(int n, int &Squared, int &Cubed);

int main()
{
	int myNum;
	int mySquared;
	int myCubed;

	cout << "Enter a number (0-20): ";
	cin >> myNum;

	int isGood = Factor(myNum, mySquared, myCubed);

	if (isGood)
	{
		cout << "Squared: " << mySquared << endl;
		cout << "Cubed: " << myCubed << endl;
	} else {
		cout << "ERROR!" << endl;
	}

	return 0;
}

int Factor(int n, int &Squared, int &Cubed)
{
	int Value = 0;
	if (n > 20 || n < 0)
	{
		Value = 0;
	} else {
		Squared = n * n;
		Cubed = n * n * n;
		Value = 1;
	}
	return Value;
}
Last edited on
References can be thought of as automatically dereferenced pointers that are always pointing to valid memory. If you use pointers, like in your first example, you need to check them for NULL. Unless someone is misusing a reference, they will never be NULL.

Also, references can be used without the asterisk. The only time I use pointers is when I am using someone else's library and their functions only take pointers. Use references whenever possible.
My thoughts exactly, thanks a lot!
References can be thought of as automatically dereferenced pointers that are always pointing to valid memory.
Just don't ever do this:
1
2
3
4
5
int* y = new int;
const int& x = (*y = 7);
cout << x << ' ';
delete y;
cout << x << endl;
Last edited on
And don't use functions to return more than one (1) value.....its considered poor practice.
Buffbill I didn't technically mean a function returning more than one value. What I am talking about is using a function to change more than 1 value in a different scope than the functions. For example..
1
2
int i, j;
MyFunc(i, j);

Using a code such as the one above to change the value of both i and j without having to define a global variable.
Last edited on
Topic archived. No new replies allowed.