return in function in c++

Jun 25, 2020 at 9:58pm
Hello,

return type in DeleteFromQueue is int.
My code is not compiled. because in "else" I should return something.
How can I fix it?

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	int DeleteFromQueue()
	{
		if (!isEmpty())
		{
			int x = Queue[rear--];
			cout << "Deleted => " << x;
			return x;
		}
		else
		{
			cout << "Queue is EMPTY ! \n";
			// we need return something. because this code doesn't compile without return type.


		}
	}
Jun 25, 2020 at 10:10pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool DeleteFromQueue(int& returnedVal)
	{
		if (!isEmpty())
		{
			returnedVal = Queue[rear--];
			cout << "Deleted => " << returnedVal ;
                        return true;
		}
		else
		{
			cout << "Queue is EMPTY ! \n";
			return false;
		}
	}


You pass a reference to an int. If the function returns true, there was something in the queue and that value is now in the int you passed by reference.
Last edited on Jun 25, 2020 at 10:10pm
Topic archived. No new replies allowed.