Difference between these two funciton

Mar 9, 2009 at 9:23pm
Item getFromList (NodePtr &hdList) {
int number;
number = hdList->data;
hdList = hdList->next;
return Item(number);
}

Item getFromList (NodePtr &hdList) {
int number;
NodePtr nowPtr;
nowPtr = hdList;
number = hdList->data;
hdList = hdList->next;
delete nowPtr;
return Item(number);
}

what is the exact difference between these two functions? Why the one below is better?
Mar 9, 2009 at 9:56pm
The bottom one deletes the node. The top one doesn't. This doesn't make it "better", it just means it does something the other doesn't. Deleting might even be bad depending on whether or not getFromList is expected to delete the retreived item.
Mar 9, 2009 at 11:04pm
yah, and IMHO a function named "get-something" performs a read-only operation; I'd be annoyed if as a side effect it modified the list.

But this is a homework question, and homework questions are not meant to teach the best programming practices; rather they try to teach specific concepts.
Topic archived. No new replies allowed.