为什么我们应该总是返回一个函数的值? [英] Why we should always return values from a function?

查看:101
本文介绍了为什么我们应该总是返回一个函数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个很大的编程人员,但是很多程序员都倾听我们应该总是从函数返回值。我想知道原因。

解决方案

一个函数不需要返回任何东西......如果你看看C(++ )函数,其中许多不(没有,也没有显式地):

  void nonReturningFunction(const int * someParam); 
int main()
{
int i = 12;
nonReturningFunction(& i);
返回0;
}
void nonReturningFunction(const int * someParam)
{
printf(我打印参数的值:%d,someParam);
}

后者不返回任何结果,它返回一个void。主函数确实返回了一些信息: 0 ,这通常是系统发出的一个信号,让它知道程序已经完成,并且完成的很好。

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



以一个班级为例,

 <?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; //< - 返回流畅接口实例
}
public function setFoo($ val = null)
{
$ this-> ; foo = $ val;
返回$ this;
}
}
$ f = new Foo();
$ f-> setFoo('foo') - > setBar('bar'); //< - 流畅界面
echo $ f-> getFoo();
?>

如果setter函数没有返回任何东西,你必须写:

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

所以在这种情况下,返回值是相关的。另一个不相关的例子:

 函数manipulateArray(array& $ arr)//<  - 通过引用传递
{
sort($ arr);
}
$ arr =范围('Z','A');
manipulateArray($ arr);
var_dump($ arr); //数组排序为

p>

 函数manipulateArray(array $ arr)//<  - 通过复制传递
{
sort($ ARR);
返回$ arr;
}
$ arr =范围('Z','A');
manipulateArray($ arr);
var_dump($ arr); //数组未排序!
$ arr = manipulateArray($ arr); //< - 要求重新分配

在许多情况下,按引用传递被认为是有风险的,这就是后一种方法通常被认为更好的原因。因此,在很多情况下,这些函数不需要返回值,但它们都是一样的,因为它使代码更安全。
这可能就是为什么你有这样的印象,即函数必须始终返回一个值。


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.

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆