( sorry on mobile) Forget the mathematics concept for a bit and use this analogy. Imagine the function is a servant that does something. For instance, you can have a servant that you give an empty glass of water and his “function” is to go fill it up and “return” it to you. Now you can have another servant that is tasked with disposing your trash, you give him the trash and he disposes of it and doesn’t need to return anything to you. But sometimes you may want the servant to say send an important letter but let’s say the post office was closed. You don’t want him to just do nothing about it, so he returns with a message telling you it’s closed and he couldn’t do the task. So similarly, a function can return you a value so you can do something about it, or they just perform a task that you don’t need anything to be returned at the end of, or to return a value or something to deal with it if it fails. Alternatively you can throw an exception but you get the point.
Latest Answers