scala中命名参数函数_Scala中的默认参数

scala中命名参数函数

Scala中的默认参数 (Default parameters in Scala)

A default parameter is a parameter that is declared with a default value in arguments. These default values are used by the functions when no value is passed by the programmer while calling the function.

默认参数是使用arguments中的默认值声明的参数 。 当程序员在调用函数时未传递任何值时,这些缺省值将由函数使用。

In it programs Scala lets its users to optionally leave passing values to the parameter. Scala allows you to specify default parameter in a function and the values for these default parameters can be omitted at the time of function call. When there is no value passed for an argument in the call of the function, default values are used.

在其程序中,Scala使其用户可以选择将传递值留给参数。 Scala允许您在函数中指定默认参数,并且在调用函数时可以忽略这些默认参数的值。 如果在函数调用中没有传递参数值,则使用默认值。

A default parameter is of the same datatype and can have any value within the range. In the case of multiple default parameter, the first one is treated as first and so on.

默认参数具有相同的数据类型,并且可以具有范围内的任何值。 如果有多个默认参数,则第一个视为第一个,依此类推。

Syntax:

句法:

    def functionName ( param : Datatype = value )

Explanation:

说明:

This syntax defines a default parameters, this function uses its own value that if the function call is without any input parameter. The value is added after the datatype of the parameter and this defines that the values used as default values.

此语法定义了一个默认参数 ,如果函数调用没有任何输入参数,则此函数将使用其自己的值。 该值被添加到参数的数据类型之后,这定义了将这些值用作默认值。

Example of default parameters:

默认参数示例:

object Demo {
   def welcome( name: String = "to Include Help" ){
      println("Welcome "+ name)
   }
   def main(args: Array[String]) {
      println("This program shows the use of default parameters.");
      welcome();
     welcome("Shivang")
   }
}

Output

输出量

This program shows the use of default parameters.
Welcome to Include Help
Welcome Shivang

Code Explanation:

代码说明:

The above code shows a basic example of default parameter usage. The code uses a string datatype parameter and has set its default value to the string " to include help" this means if no value is passed as parameter the code will print "Welcome to include help" as shown in output otherwise will print the user name with a welcome message. This types of code are often used by we try to reduce error or NULL value situations.

上面的代码显示了默认参数用法基本示例 。 该代码使用字符串数据类型参数,并将其默认值设置为字符串“包括帮助”,这意味着如果未将任何值作为参数传递,则代码将输出“欢迎包括帮助” ,如输出所示,否则将打印用户名带有欢迎信息。 我们尝试使用这种类型的代码来减少错误或NULL值的情况。

翻译自: https://www.includehelp.com/scala/default-parameters-in-scala.aspx

scala中命名参数函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值