scala中函数带多个参数
具有可变参数的函数 (Function with variable arguments)
A function takes some variables in its definition as arguments that are passed when it is called. The function uses these arguments to calculate some results for which the function is used. Sometimes there are cases when we do not know how many arguments will be passed to the function in the runtime?
函数在其定义中将一些变量作为调用时传递的参数。 函数使用这些参数来计算使用该函数的一些结果。 有时在某些情况下,我们不知道在运行时会将多少个参数传递给该函数?
For these conditions, one approach can be redefining the same function ( overloading the function ) for every case. But this method is not that efficient. So, for this case programming languages define varargs that are used to take variable arguments in the function call.
对于这些情况,一种方法是针对每种情况重新定义相同的功能(使功能超载)。 但是这种方法效率不高。 因此,在这种情况下,编程语言定义了用于在函数call中采用变量参数的 varargs 。
In Scala, also variable arguments are used. In Scala, use have to indicate that the last argument can be repeated i.e. it can be any variable. The program treats this as an array of that datatype and expects the user to input multiple arguments which can be treated as an array. To show the compiler that this function is going to accept multiple arguments * is used. This asterisk symbol symbolizes that the argument is an array of the specified data type.
在Scala中,还使用可变参数 。 在Scala中,必须使用来指示可以重复最后一个参数,即它可以是任何变量。 该程序将其视为该数据类型的数组,并期望用户输入多个可以视为数组的参数。 为了向编译器显示此函数将接受多个参数,使用* 。 此星号符号表示该参数是指定数据类型的数组。
For example, Strings* symbolizes an array of Strings.
例如, Strings *表示字符串数组。
Syntax:
句法:
def functionName(args: String*){
//code to be executed
}
Explanation:
说明:
The preceding Syntax initializes a function functionname(), this function uses variable arguments of the datatype string in its definition.
前面的语法初始化函数functionname() ,此函数在其定义中使用数据类型字符串的变量参数 。
Example code:
示例代码:
object myClass {
def printStrings( arguments:String* ) = {
var i : Int = 0;
println("The array of arguments recieved is : ")
for( string <- arguments ){
println("Arg value[" + i + "] = " + string );
i = i + 1;
}
}
def main(args: Array[String]) {
println("This program demonstrate use of variable arguments. ")
println("first Run :")
printStrings("Includehelp", "is", "awesome");
println("Second Run :")
printStrings("I", "Love", "programming", "in", "scala");
}
}
Output
输出量
This program demonstrate use of variable arguments.
first Run :
The array of arguments recieved is :
Arg value[0] = Includehelp
Arg value[1] = is
Arg value[2] = awesome
Second Run :
The array of arguments recieved is :
Arg value[0] = I
Arg value[1] = Love
Arg value[2] = programming
Arg value[3] = in
Arg value[4] = scala
Code explanation:
代码说明:
The preceding code uses a function printstrings() that takes variable arguments (varargs). And prints each of their values in separate lines. In the main function, The code has called the function printstrings() two times. The first time with arguments " 'includehelp', 'is', 'awesome' " it takes three arguments in the array this time. The second time the function is called with arguments " I love programming in Scala" this time the number of arguments is 5. In both cases, the program executes and runs properly.
前面的代码使用带有可变参数(varargs)的函数printstrings( ) 。 并将它们的每个值打印在单独的行中。 在主函数中,代码两次调用了函数printstrings() 。 第一次使用参数“'includehelp','is','awesome'”时,这次需要在数组中使用三个参数。 这次第二次使用参数“我喜欢在Scala中编程”来调用该函数,此时参数的数量为5。在两种情况下,程序都可以正常执行并运行。
THE MAIN FUNCTION
主要功能
The main() function in Scala also uses varargs to take multiple command line arguments. Now, as we are familiar with the functioning of variable arguments, we can use command line arguments in the main function to give input to our code.
Scala中的main()函数还使用varargs接受多个命令行参数。 现在,由于我们熟悉变量参数的功能 ,因此可以在主函数中使用命令行参数为代码提供输入。
翻译自: https://www.includehelp.com/scala/function-with-variable-arguments-in-scala.aspx
scala中函数带多个参数