Why use only return, without returning any data?

6

Studying the code of a framework I came across the following code snippet:

if (file_exists('lib/util/' . $className . '.php'))
{
     include 'lib/util/' . $className . '.php';
     return;
}

What is the purpose of using return without having any variables or expressions to return? Is it possible to do this only in PHP or other languages like Java as well?

    
asked by Leonardo Vilarinho 28.05.2016 в 15:42

2 answers

7

There are cases where you just want to run a routine, follow the algorithm, a procedure, without generating results. It does an action but does not produce a value to be used in an expression. Each language can give a name and a way to handle it.

This is possible in virtually all languages. Some require that this algorithm has a different name (% with%, for example). I'm talking about this in What's the difference between functions and procedures? .

Mathematically speaking a function should return some value. Many languages allow them to not return because there is not much practical advantage to give another name just because it does not return a value.

In statically typed languages, normal is to define a return convention depending on its syntax. Some require the type to be procedure (Java, for example) or some other name. In fact this is not usually a data type effectively.

In dynamic typing languages it is normal for the function not to have a type, so returning a value or not, for the compiler, gives the same. Of course, if you return "nothing" because you expect the return of a value, it will be a logical error. I have already seen several cases of the function being confused and starting returning a value of one type, then there is a maintenance and it creates another execution path and returns no value. In dynamic (not all) languages this is usually possible since returning nothing is actually returning a null, which is still a value.

In theory, these procedures do not require a void command. If it exists, it obviously should not return value (or at least it will be discarded in static languages). Of course there are cases that the procedure needs to finish before its last line. A return command is for flow control and is used to terminate execution immediately.

Some languages, even in full functions (that return value), allow return to be omitted to produce a result. But of course the command will always be needed if you want to stop execution before its end. And this is the primary goal of all return , returning a value is just its secondary function. The word "return" should be read as "return to the calling location" and not "return a value". For this reason it even has language that even uses the variable return as the default to identify what should be used as a result at the end of the function. Although everyone understands "return a value" the correct one is "return a value and return to the caller" .

Some languages allow for more than one value (usually an internal trick).

There on the right side you have an important related question on the subject.

Wikipedia article on Result . About the return type .

    
28.05.2016 / 16:12
2

It is possible. For example, the print function is a function that does not return anything, it just runs the print (equivalent to the include in your example) and finishes.

    
28.05.2016 в 15:46