Need advice with Functions returning a value.

Ok so heres my homework assignment:

"Rewrite a the findMax() function in Program 6.5 so that the variable max, declared in main(), is used to store the maximum value of the two passed numbers. The value of max should be set from within findMax().

And heres my 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
26
27
28
29
30
#include <iostream>
using namespace std;

int findMax(int, int, int&);

int main()
{
	int firstnum, secnum, max;
	
	cout << "\nEnter a number: ";
	cin >> firstnum;
	cout << "Great! Please enter a second number: ";
	cin >> secnum;
	
	findMax(firstnum, secnum, max);
	cout << "\nThe maximum of the two number is " << max << endl;
	
	return 0;
}

int findMax(int x, int y, int& maxnum)
{
	
	if (x >= y)
		maxnum = x;
	else
		maxnum = y;
	
	return;
}


Im not sure why its not compiling. Im getting this error:

G:\cpp>g++ findMaxFunction.cpp -o findMaxFunction.exe
findMaxFunction.cpp: In function `int findMax(int, int, int&)':
findMaxFunction.cpp:29: error: return-statement with no value, in function returning 'int'

Thanks for any and all help =)
Last edited on
The error message is self explanatory:
return-statement with no value, in function returning 'int'

You need to return a value, not to change the argument value
Shouldn't it be returning maxnum how it is?

Anyways I changed the type of findMax to void and it worked. Can anyone explain why?
Because void means no return type.
Should your function return maximum or change its value? These are different things...
Im pretty sure the function findMax should return maxnum to the main() as max if that makes any sense.
So you need to return a value. These are the two options:
1
2
3
4
5
6
7
8
9
10
11
12
void findMax(int x, int y, int& maxnum)
{
   // modify maximum
   // don't return anything
}

int findMax(int x, int y)
{
   int maximum;
   // ...
   return maximum;
}
Im kinda lost haha, the wording of the question is a little confusing.

All the exaples aren't using

1
2
 
return maximum;


they just use return;

ill try and breakdown what it needs to do.
1.) Get user input for firstnum, secnum

2.)Call the function findMax using firstnum for x and secnum for y and calculate the maxnum.

3.)Change the value of max in main() to the value of maxnum in the function.
The value of max should be set from within findMax().
Try the void one, or overload the function both ways
Thank you I got it to work. Now Ive been stuck on this random generator problem for a while. Im sorry if im a pain haha. Im just having problems with functions. :

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
#include <iostream>
using namespace std;

void randnum(float, float&);

int main()
{
	int num = 0;
	float number = 0;
	float seed = 0;
	
	cout << "Enter a six-digit integer (not divisible by 2 or 5) : ";
	cin >> num;	
	randnum(seed, number&);
	cout << "\nRandom Number: " << number << endl;
	
	return 0;
}

void randnum(float i, float &x)
{


	i = int(997.0 * x / 1.e6);
	x = 997.0 * x - i * 1.e6;
	
	return;
}


Its using power-residue. Heres the steps:

1.) Have a user enter a six-digit odd integer seed that isnt divisible by 2 or 5
2.) Multiply the seed by the number 997.
3.) Extract the lower six digits of the result produced by step 2. Use this random number as the next seed.
4.) Repeat steps 2 and 3 for as many random numbers as needed.

The actual part im stuck on:

a.) create a funtion named randnum() that accepts a floating-point "seed" as a parameter and returns a floating-point random number between 0 and 1.e6.

Im having a problem getting randnum() to set the value (x) of number in main().
Last edited on
randnum(seed, number&);

You don't want the '&' after number, when passing by reference you don't have to do anything special to the variable.
When I do that i get this output:

Enter a six-digit integer (not divisible by 2 or 5) : 654321

Random Number: 0

the actual random number returned should be 358037 after doing calculations by hand.

So I dont know why it wont set x in the randnum function to number in the main function.

fixed up the code a little but it still isnt working:

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
#include <iostream>
using namespace std;

void randnum(float, float&);

int main()
{
	float number = 0;
	float seed = 0;
	
	cout << "Enter a six-digit integer (not divisible by 2 or 5) : ";
	cin >> seed;	
	randnum(seed, number);
	cout << "\nRandom Number: " << number << endl;
	
	return 0;
}

void randnum(float i, float& x)
{


	i = int(997.0 * x / 1.e6);
	x = 997.0 * x - i * 1.e6;
	
	return;
}

Last edited on
Topic archived. No new replies allowed.