Why return a bool from an insert function?

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?
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.
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.
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.
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
Topic archived. No new replies allowed.