scala 函数调用

函数按名称调用 (Functions call by name )

By default, the method of parameter passing in a programming language is "call by value". In this, the parameter is passed to a function which makes a copy of them an operates on them. In Scala also, the call by name is the default parameter passing method.

默认情况下,以编程语言传递参数的方法是“按值调用” 。 在这种情况下,参数被传递给一个函数,该函数使它们的副本对它们进行操作。 同样在Scala中,按名称调用是默认的参数传递方法。

Call by name is used in Scala when the program needs to pass an expression or a block of code as a parameter to a function. The code block passed as call by name in the program will not get executed until it is called by the function.

当程序需要将表达式或代码块作为参数传递给函数时, 在Scala中使用按名称调用 。 在程序中按名称传递作为调用传递的代码块,直到函数调用该代码块后,才能执行。

Syntax:

句法:

    def functionName(parameter => Datatype){
//Function body... contains the call by name call to the code block
}

Explanation:

说明:

This syntax initializes a call by name function call. Here the function's argument passed is a function and the datatype is the return type of the function that is called. The function body executes the call by name call to the function that evaluates to provide the value. The call is initiated by using the parameter name as specified in the program.

语法通过名称函数call初始化调用 。 这里传递的函数参数是一个函数,数据类型是所调用函数的返回类型。 函数主体通过名称调用执行对要评估以提供值的函数的调用。 通过使用程序中指定的参数名称来启动该调用。

Example:

例:

object Demo {def multiply(n : Int) = {(14*5);
}
def multiplier( t: => Long ) = {println("Code to multiply the value by 5")
println("14 * 5 = " + t)
}
def main(args: Array[String]) {println("Code to show call by name")
multiplier(multiply(14))
}
}

Output

输出量

Code to show call by name
Code to multiply the value by 5
14 * 5 = 70

Code explanation:

代码说明:

The above code is to display the use of call by name. The code prints the number multiplied by 5. The number in the code is 14. That is passed to the call by name function at the time of function call form the main call. The in the multiplier function after the code it needs to execute the multiply method is initiated and value is evaluated there to be returned to the calling function which prints the value i.e. 70.

上面的代码是按名称显示呼叫使用 。 该代码将打印乘以5的数字。代码中的数字为14。在从主调用进行函数调用时,该函数将传递给按名称调用。 在乘法器函数中,需要执行乘法方法的代码启动后,在其中求值,然后返回到打印该值(即70)的调用函数。

翻译自: https://www.includehelp.com/scala/functions-call-by-name-in-scala.aspx

scala 函数调用

scala 函数调用_在Scala中按名称调用函数相关推荐

  1. python不同文件函数调用_不同输入文件的多处理调用函数

    当进行多处理时,每个子进程都会获得主模块中在if __name__ == '__main__':语句之前定义的所有全局变量的副本.{{cdm>中访问的每个进程都将是不同的.在 一种解决方法是使用 ...

  2. python调用函数怎么错_在Python中从类调用函数时参数数目错误

    我试图用python编写一个遗传算法的实现.上面写着我用两个参数来调用它,而只有一个是允许的,但我肯定我不允许.在 以下是相关代码:class GA: def __init__(self, best, ...

  3. scala并发_探索Scala并发

    2003年,Herb Sutter在他的"免费午餐结束了"一文中揭露了业界最大的"肮脏小秘密",清楚地表明,越来越快的处理器时代已经结束,将由一个新的并行化时代 ...

  4. python 函数调用列表_python – 根据列表中的字符串调用函数

    Terms: talib: Technical Analysis Library (stock market indicators, charts etc) CDL: Candle or Candle ...

  5. lisp调用qleader端点_[分享]lisp中可用command调用的函数全集!!!!

    [分享]lisp中可用command调用的函数全集!!!! [em21] [em16] lisp中command命令可以调用的函数全集: 3D 创建三维多边形网格对象 3DARRAY 创建三维阵列 3 ...

  6. python平方数迭代器_对python中的高效迭代器函数详解

    python中内置的库中有个itertools,可以满足我们在编程中绝大多数需要迭代的场合,当然也可以自己造轮子,但是有现成的好用的轮子不妨也学习一下,看哪个用的顺手~ 首先还是要先import一下: ...

  7. pythonpandas函数详解_对pandas中Series的map函数详解

    Series的map方法可以接受一个函数或含有映射关系的字典型对象. 使用map是一种实现元素级转换以及其他数据清理工作的便捷方式. (DataFrame中对应的是applymap()函数,当然Dat ...

  8. python 实例方法调用时可以忽略第一个参数_盘点Python中易忽略的函数

    Python 内置函数 1.ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数, ...

  9. python enumerate函数_关于python中enumerate和zip函数的用法及举例

    关于python中enumerate和zip函数的用法及举例 关于enumerate函数: enumerate函数可以同时返回列表或元组等可迭代对象的下标和内容,但实际上,enumerate函数实际返 ...

最新文章

  1. 图像分割--PixelNet: Representation of the pixels, by the pixels, and for the pixels
  2. 面试看缘分php,php测试姓名缘分指数
  3. 操作ex_【自学C#】|| 笔记 40 Command:操作数据库
  4. angularjs -- 路由监听
  5. Log4j的自定义logger
  6. CSS中可继承的属性
  7. [导入]写了个img2ppt的demo,为写PDF2PPT做准备
  8. log4j.xml的实用例子
  9. Ucenter通信原理
  10. 终于找到可转载的摄影基础知识贴了
  11. 古籍排版 -古籍重制 - 杨式梨花枪 - 古籍制作 - 古籍制作工具 - 古籍制作软件 - 古籍修复工具 - 古书制作 - 古书制作软件 - 古书修复工具,
  12. Android 开发的五大开源网站
  13. 解决使用Glide加载图片背景出现浅绿色
  14. 克拉夫斯曼高端定制 刘霞---【YBC中国国际青年创业计划】
  15. 【数据攻略】字节面试真题(含答案)+100道面试题库
  16. 在3D空间中绘制四边形
  17. 守护线程setDaemon的理解
  18. Cesium空间分析、Cesium通视分析
  19. 股票跌得猝不及防,是因为你还不懂这个技术!
  20. 淘宝二手优必选舵机保姆级驱动教程,看不懂来打我(自行修改ID,有HAL库驱动函数)

热门文章

  1. comcerter无法识别串口_基于FPGA 的MXN维字符识别的实现
  2. 西工大java高级网络编程_奥鹏西工大16春《JAVA高级网络编程》平时作业
  3. node中的缓存机制
  4. 使用 Canvas 生成公众号头图
  5. js 实现文件导出、文件下载
  6. Ansible-----条件判断与错误处理
  7. 实现 通过数据库里一个字段值相等 则把 他合为一条数据
  8. 2018-04-22接口自动化测试学习心得(1)
  9. pat 甲级 1072. Gas Station (30)
  10. 解决SimpleButton被移除后保持OVER状态