Search code examples
phpfunctionreturn

Why we should always return values from a function?


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.


Solution

  • A function needn't return anything... If you look at C(++) function, many of them don't (well, not explicitly):

    void nonReturningFunction(const int *someParam);
    int main()
    {
        int i = 12;
        nonReturningFunction(&i);
        return 0;
    }
    void nonReturningFunction(const int *someParam)
    {
        printf("I print the value of a parameter: %d",someParam);
    }
    

    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:

    <?php
        class Foo
        {
            private $foo,$bar;
            public function __construct()
            {
                $this->foo = 'bar';
            }
            public function getFoo()
            {
                return $this->foo;//<-- getter for private variable
            }
            public function getBar()
            {
                return $this->foo;//<-- getter for private variable
            }
            public function setBar($val = null)
            {
                $this->bar = $val;
                return $this;//<-- return instance for fluent interfacing
            }
            public function setFoo($val = null)
            {
                $this->foo = $val;
                return $this;
            }
        }
        $f = new Foo();
        $f->setFoo('foo')->setBar('bar');//<-- fluent interface
        echo $f->getFoo();
    ?>
    

    If the setter function didn't return anything, you'd have to write:

    $f->setFoo('foo');
    $f->setBar('Bar');
    

    So in this case, return values are relevant. Another example where they're irrelevant:

    function manipulateArray(array &$arr)//<-- pass by reference
    {
        sort($arr);
    }
    $arr = range('Z','A');
    manipulateArray($arr);
    var_dump($arr);//array is sorted
    

    as opposed to:

    function manipulateArray(array $arr)//<-- pass by copy
    {
        sort($arr);
        return $arr;
    }
    $arr = range('Z','A');
    manipulateArray($arr);
    var_dump($arr);//array is not sorted!
    $arr = manipulateArray($arr);//<-- requires reassign
    

    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.