Which is better function() return; or function(reference)

Which method is better to use? return or reference or is it just a preference thing.

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
  #include <iostream>
#include <stdlib.h>
using namespace std;
unsigned int returnfunction(unsigned int);
void referencefunction(unsigned int&);
int main()
{	
	unsigned int number;
	string option, numberstr, error;
	do {
		cout << "Would you like to use return method or reference(1 or 2)?" << flush;
		getline(cin, option);
	} while(option.size() > 1 || option.size() < 1 || (option[0] != '1' && option[0] != '2'));
	do {
		cout << "Please enter a number: " << flush;
		getline(cin, numberstr);
		for(unsigned int i = 0; i < numberstr.size(); i++) if(isalpha(numberstr[i])) error = "true"; else error = "false";
	} while(error == "true");
	number = atoi(numberstr.c_str());
	switch(option[0]){
		case '1': number = returnfunction(number); cout << "Returned: " << number << endl; break;
		case '2': referencefunction(number); cout << "Reference: " << number << endl; break;
	}
}

unsigned int returnfunction(unsigned int a)
{
	unsigned int b = ++a;
	return(b);
}

void referencefunction(unsigned int &a)
{
	a++;
}
Returning a value offers more flexibility in usage, often times making it more simplistic to use... and therefore should be preferred.

Especially if you don't want the passed variable to be modified.
Alright thanks and can you return multiple values? because If you are modifying mulitple variables it is easy using references eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 void function(unsigned int&, unsigned int&);
int main()
{
unsigned int num1 = 0, num2 = 0;
cout << "Before: " << endl;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
function(num1, num2);
cout << "After: " << endl;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
}
void function(unsigned int &a, unsigned int &b)
{
a++;
b++; b++;
}



Last edited on
can you return multiple values?


No. The closest you can do to that is return a class/struct instance which contains multiple values.... but that's often less convenient than passing by reference.


Though the need to output multiple values isn't as common as you might be thinking.
Yeah I can't think of too many scenario's I was just wondering because I was making a simple "final project" for my class at school and I needed to return 2 values one for if the guess was too high, and one for if the guess was too low. here's what I did:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <stdlib.h>
void mainmenu(std::string);
void start(std::string);
void check(std::string&);
unsigned int convert(std::string);
unsigned int rand(unsigned int, unsigned int);
void guessed(std::string, unsigned int, char&, unsigned int&, unsigned int&);
void quit(std::string);

int main()
{
	std::string name;
	do {
		std::cout << "What is your name? " << std::flush;
		std::getline(std::cin,name);
		for(unsigned int i = 0; i<name.size(); i++) if(!isalpha(name[i])) name = "false";
		if(name == "false") std::cout << "Invalid name.\n" << std::flush;
	} while(name == "false");
	std::cout << "I'm glad you could play my game " << name << '\n' << std::flush;
	mainmenu(name);
	return(0);
}

void mainmenu(std::string a)
{
	std::string input;
	do {
		std::cout << a << " please enter \"P\" to play.\n" << a << " please enter \"Q\" to quit.\nChoice: " << std::flush;
		std::getline(std::cin,input);
		for(unsigned int i = 0; i<input.size(); i++) input[i] = toupper(input[i]);
		if(input != "P" && input != "Q") std::cout << "Invalid choice " << a << ".\n" << std::flush;
	} while(input != "P" && input != "Q");
	switch(input[0]){
		case 'P': start(a); break;
		case 'Q': quit(a); break;
	}
}

void start(std::string a)
{
	std::string low, high, guess;
	unsigned int l, h, r, i = 0;
	char g;
	do {
		std::cout << a << " what range would you like?(ex. 1-100)...\nLowest Value: " << std::flush;
		std::getline(std::cin,low);
		std::cout << "Highest Value: " << std::flush;
		std::getline(std::cin,high);
		check(low);
		check(high);
		l = convert(low);
		h =convert(high);
		if(low == "false" || high == "false" || l >= h) std::cout << "Invalid range " << a << ".\n" << std::flush;
	} while(low == "false" || high == "false" || l >= h);
	
	r = rand(l, h);
	
	do {
		i++;
		std::cout << a <<" please guess a number between " << l << " and " << h << "\nGuess " << i << ": " << std::flush;
		std::getline(std::cin,guess);
		check(guess);
		if(guess != "false") guessed(guess, r, g, l, h);
	    else std::cout << "Invalid guess " << a << '\n' << std::flush;
	} while (g != 't');
	if(i != 1) std::cout << "\nGreat job " << a << "!\nIt only took you " << i << " tries to guess the random number \"" << r << "\".\n\n" << std::flush;
	else std::cout << "\nGreat job " << a << "!\nIt only took you " << i << " try to guess the random number\"" << r << "\".\n\n" << std::flush;
	mainmenu(a);
}

void check(std::string &a)
{
	for(unsigned int i = 0; i<a.size(); i++) if(!isdigit(a[i])) a = "false";
}

unsigned int convert(std::string a)
{
	unsigned int b;
	b = atoi(a.c_str());
	return(b);
}

unsigned int rand(unsigned int a, unsigned int b)
{
	unsigned int c;
	srand(time(0));
	c = rand() % (b - a + 1) + a;
	return(c);
}

void guessed(std::string a, unsigned int b, char &c, unsigned int &d, unsigned int &e)
{
	unsigned int guess;
	guess = convert(a);
	if(guess < d || guess > e) std::cout << "Invalid guess.\n" << std::flush;
	if(guess > b && guess < e){ std::cout << "You guessed too high.\n" << std::flush; e = guess - 1; }
	if(guess < b && guess > d){ std::cout << "You guessed too low.\n" << std::flush; d = guess + 1; }
	if(guess == b) c = 't';
}

void quit(std::string a)
{
	std::cout << "Good bye " << a << ".\nPlease play again!" << std::flush;
}

and I did the std:: because I figure it will compile slightly faster than if you do the using namespace std since I figured that would include all the unnecessary std's eg std::vector but the program is very small so it probably would be unnoticable.

one more question though disch when would you EVER use a static number I did an experiment with them with this code:
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
#include <iostream>
using namespace std;

void staticintfunc(void);
void autointfunc(void);

int main(void)
{
	for(unsigned int i = 0; i<5; i++) staticintfunc();
	for(unsigned int i = 0; i<5; i++) autointfunc();
}

void staticintfunc(void)
{
	static int a = 0;
	a++;
	cout << "Static: " << a << endl;
}

void autointfunc(void)
{
	unsigned int a = 0;
	a++;
	cout << "Auto: " << a << endl;
}
Topic archived. No new replies allowed.