Why return a bool from an insert function?

Oct 22, 2015 at 4:20am
I'm writing a function to insert a Node into a Tree. The assignment calls for the insert function to return a boolean. I don't understand why I should return a boolean, or how I should do so in the first place?
Oct 22, 2015 at 4:22am
If it doesn't specify what the exact value of the return value should be, then you can just return true.

If your instructor complains, he is wrong.
Oct 22, 2015 at 4:26am
Perhaps if the tree has a maximum size or depth, then it would make sense to return false if the new node can't fit.
Oct 22, 2015 at 6:16am
The whole point of your professor making you make an insert() function return a bool is this:

did your value get inserted properly into the container? The complexities are left up to you, like does the node point to null or is there not enough physical memory to actually add another node? etc.
Oct 22, 2015 at 8:29am
Ok, so there seem to be 2 cases where inserting a node can fail, therefore returning false:

1) Not enough space in memory or container
2) Invalid input data

1) If a container is of a limited size, then return false when it is full. If there is not enough memory, return false when memory allocation fails.

2) Check the input parameters of the insert function. If they are not valid for some reason, return false.
Last edited on Oct 22, 2015 at 8:30am
Topic archived. No new replies allowed.