Programming Functions and why are they so special

793 views

I’m still trying to wrap my ahead around this concept.

I’ve done basic maths, so I know that f(x) = x is like a function, but I can’t find an intuitive way of explaining why some functions don’t have to return values. In addition, what separates functions from just lines of code?

In: Technology

12 Answers

Anonymous 0 Comments

( 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.

You are viewing 1 out of 12 answers, click here to view all answers.