Blogger Information
Blog 7
fans 0
comment 0
visits 3927
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php编程基础0806作业
Lank的博客
Original
999 people have browsed it

请实例演绎你对回调函数与递归函数的理解?

1)回调函数

网络上的定义:回调函数就是一个被作为参数传递的函数。在C语言中,回调函数只能使用函数指针实现,在C++、Python、ECMAScript等更现代的编程语言中还可以使用仿函数或匿名函数。
实例1
跟女孩子约会,回家后让女孩跟我发短信,回到家这个过程不确定,我是主线程,回过头来调用,不耽误主线程要做的事。
主线程和其他线程,以主线程为主

  1. <!-- 匿名函数作为回调函数 -->
  2. <?
  3. $func=function($a,$b){
  4. return $a+$b;
  5. };
  6. function test(Closure $callback){
  7. $a=10;
  8. $b=20;
  9. echo $callback($a,$b);
  10. };
  11. echo test($func);
  12. ?>

效果如下:

php脚本是单线程

耗时函数

  1. <!-- 匿名函数作为回调函数 -->
  2. <?
  3. $func=function($a,$b){
  4. return $a+$b;
  5. };
  6. function test(Closure $callback){
  7. $a=10;
  8. $b=20;
  9. echo $callback($a,$b);
  10. };
  11. echo test($func);
  12. ob_clean();
  13. function demo(string $name):string{
  14. return "你好 ".$name;
  15. };
  16. echo call_user_func('demo',"lank");
  17. ?>

效果如下

2)递归函数

编程语言中,函数Func(Type a,……)直接或间接调用函数本身,则该函数称为递归函数。递归函数不能定义为内联函数。

在数学上,关于递归函数的定义如下:对于某一函数f(x),其定义域是集合A,那么若对于A集合中的某一个值X0,其函数值f(x0)由f(f(x0))决定,那么就称f(x)为递归函数。

老师的讲解:函数自身调用自身,但必须在调用自身前有条件判断,否则无限无限调用下去。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:实际上这个回调并非异步的
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!