return doesn't work but cout does?

Apr 3, 2016 at 10:06pm
this function from my class returns the size of length of the linked list.

1
2
3
4
5
6
7
8
9
10
11
12
  int LinkedList::GetLength()
{
	int count = 0;
	Node* temp = head;
	while (temp!=0)
	{
		count++;
		temp = temp->next;
	}
	//cout << count;     <-- this works
	return count;
}
Apr 3, 2016 at 10:14pm
What do you mean doesn't work? Does the function not return the same value that is printed?
Apr 3, 2016 at 10:14pm
it doesnt return anything at all. just a blank space
Apr 3, 2016 at 10:20pm
That's impossible. It's returning an int, which will be 4 (or eight) bytes in size, and that memory will contain some value because it's impossible for it to not contain some value.

So what makes you think it's somehow a blank space?
Apr 3, 2016 at 10:23pm
Note that return doesn't automatically print the value. To print the value you need to do it explicitly.

 
cout << yourList.GetLength();
Last edited on Apr 3, 2016 at 10:24pm
Apr 3, 2016 at 10:25pm
output with return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
5
7
9
11
5

5
7
9
11
5
7

Press any key to continue . . .


output with cout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
5
7
9
11
5

5
7
9
11
5
7

6Press any key to continue . . . //notice the 6 over there? 
Apr 3, 2016 at 10:29pm
@peter

jesus christ. i can't believe i forgot that. im so focused on this thing i forgot the basics. so bad. thanks. DX
Topic archived. No new replies allowed.