沃梦达 / 编程问答 / php问题 / 正文

为什么我们应该总是从函数返回值?

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.

推荐答案

一个函数不需要返回任何东西...如果你看一下 C(++) 函数,它们中的许多都没有(嗯,没有明确地):

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);
}

后者什么也不返回,好吧,它返回一个空.main 函数确实返回了一些东西:0,这通常是给系统的一个信号,让它知道程序已经完成,并且它完成得很好.

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.

同样的逻辑适用于 PHP 或任何其他编程语言.有些函数的返回值是相关的,另一个函数可能不需要返回任何东西.一般来说,函数会返回值,因为它们与程序的流程相关.

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.

以上课为例:

<?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();
?>

如果 setter 函数没有返回任何内容,则必须编写:

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

相对于:

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.

这篇关于为什么我们应该总是从函数返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么我们应该总是从函数返回值?

基础教程推荐