I am not a big programming guy but have listen from programmers a lot of times that we should always return values from a function. I want to know the reason.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A function needn’t return anything… If you look at C(++) function, many of them don’t (well, not explicitly):
The latter returns nothing, well, it returns a void. The main function does return something:
0, this is generally a signal to the system to let it know the programme is finished, and it finished well.The same logic applies to PHP, or any other programming language. Some functions’ return value is relevant, another function may not be required to return anything. In general functions return values because they are relevant to the flow of your programme.
Take a class, for example:
If the setter function didn’t return anything, you’d have to write:
So in this case, return values are relevant. Another example where they’re irrelevant:
as opposed to:
Passing by reference is deemed risky in many cases, that’s why the latter approach is generally considered to be better. So in many cases the functions needn’t return a value, but they do all the same because it makes the code safer overall.
That might be why you’re under the impression that functions must always return a value.