scala中命名参数函数_Scala中带有命名参数的函数

scala中命名参数函数

具有命名参数的函数 (Functions with named arguments )

A function is Scala can take multiple arguments. These arguments are traditionally called in sequence while calling a function. But in Scala programming, the program is given the power to alter the traditional sequence of arguments. Scala provides its users named arguments these are used to change the order of using arguments at call.

一个函数是Scala可以接受多个参数。 这些参数通常在调用函数时按顺序调用。 但是在Scala编程中,程序有权更改传统的参数顺序。 Scala为用户提供了命名参数,这些参数用于更改调用时使用参数的顺序。

Suppose a function that has two variables var1 and var2. If we want to initialize var2 first then the following syntax is used.

假设一个函数具有两个变量var1和var2 。 如果我们要首先初始化var2,则使用以下语法。

Syntax:

句法:

    functionName ( var2 = value2, var2 = value1 );

Explanation:

说明:

This will pass the value2 to the second argument in the list. And value1 in the first argument in the list.

这会将value2传递给列表中的第二个参数。 并在列表中的第一个参数中使用value1。

Example:

例:

object Demo {
    def sub( a:Int, b:Int ) = {
      println("Substraction = " + (a-b) );
   }
   def main(args: Array[String]) {
             println("The fucntion is called using named function call")
             sub(b = 5, a = 7);
   }
 }

Output

输出量

The fucntion is called using named function call
Substraction = 2

Explanation:

说明:

This code displays how to use named arguments in Scala? The code initializes a function named sub(), it expects two arguments and substracts second from first. At function call, the arguments are filled using the names that initialize them in the order the programmer wants.

此代码显示如何在Scala中使用命名参数? 该代码初始化了一个名为sub()的函数,它需要两个参数并从第一个减去第二个。 在函数调用时,将使用按程序员想要的顺序对其进行初始化的名称来填充参数。

翻译自: https://www.includehelp.com/scala/functions-with-named-arguments-in-scala.aspx

scala中命名参数函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值